Search
 
SCRIPT & CODE EXAMPLE
 

C

dynamic memory in c

// Program to calculate the sum of n numbers entered by the user

#include <stdio.h>
#include <stdlib.h>

int main() {
  int n, i, *ptr, sum = 0;

  printf("Enter number of elements: ");
  scanf("%d", &n);

  ptr = (int*) malloc(n * sizeof(int));
 
  // if memory cannot be allocated
  if(ptr == NULL) {
    printf("Error! memory not allocated.");
    exit(0);
  }

  printf("Enter elements: ");
  for(i = 0; i < n; ++i) {
    scanf("%d", ptr + i);
    sum += *(ptr + i);
  }

  printf("Sum = %d", sum);
  
  // deallocating the memory
  free(ptr);

  return 0;
}
Comment

dynamic memory allocation c

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
    // This pointer will hold the
    // base address of the block created
    int* ptr;
    int n, i;
 
    // Get the number of elements for the array
    printf("Enter number of elements:");
    scanf("%d",&n);
    printf("Entered number of elements: %d
", n);
 
    // Dynamically allocate memory using malloc()
    ptr = (int*)malloc(n * sizeof(int));
 
    // Check if the memory has been successfully
    // allocated by malloc or not
    if (ptr == NULL) {
        printf("Memory not allocated.
");
        exit(0);
    }
    else {
 
        // Memory has been successfully allocated
        printf("Memory successfully allocated using malloc.
");
 
        // Get the elements of the array
        for (i = 0; i < n; ++i) {
            ptr[i] = i + 1;
        }
 
        // Print the elements of the array
        printf("The elements of the array are: ");
        for (i = 0; i < n; ++i) {
            printf("%d, ", ptr[i]);
        }
    }
 
    return 0;
}
Comment

dynamic memory allocation

int *p = new int; // request memory
*p = 5; // store value

cout << *p << endl; // Output is 5

delete p; // free up the memory

cout << *p << endl; // Output is 0
Comment

dynamic memory allocation

Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. Reasons and Advantage of allocating memory dynamically: When we do not know how much amount of memory would be needed for the program beforehand.
Comment

PREVIOUS NEXT
Code Example
C :: link list c 
C :: linked list using c 
C :: check whether a number is prime or not in c 
C :: malloc 
C :: unsigned char c programming 
C :: continue statement in c 
C :: how to take comma separated integer input in c 
C :: c program for swapping of two numbers 
C :: How to copy one string into another in C 
C :: include ‘<stdlib.h’ or provide a declaration of ‘exit’ 
C :: unpack and repack deb package 
C :: c strcmp 
C :: access 2d array with pointer c 
C :: print in c 
C :: Relational Operator in C language 
C :: ternary operator in c 
C :: check for duplicates c 
C :: sphinx-doc 
C :: c convert float to int 
C :: pathlib exclude hidden file 
C :: metw.cc 
C :: router solicitation and advertisement magic is used by 
C :: C #if, #elif and #else Directive 
C :: FILE* fptr = fopen("test", "r"); if (__ (fptr)) { printf("End of file reached"). (42); } 
C :: temperature sensor data 
C :: Multi Select with icons htm; 
C :: denomination counter 
C :: C linked sorted lists 
C :: maximum, minimum, mean, and median of the data set. in array c programming 
C :: unconstrained box flutter 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =