The process of allocating memory during program execution is called dynamic memory allocation.
malloc() is used to allocate space in memory during the execution of the program. malloc() function does not initialize the memory allocated during execution, it carries garbage value. malloc() function return null pointer if it could not able to allocate requested amount of memory.
EX:WAP for dynamic memory allocation using malloc()
#include<stdio.h;> #include<conio.h;> main() { int i,n,*ptr,sum=0; clrscr(); printf("Enter number of elements\n"); scanf("%d",&n); ptr= (int*)malloc(n* sizeof(int)); if(ptr==NULL) { printf("memory does not exist\n"); exit(0); } printf("Enter elements in pointer variable\n"); for(i=0;i<n;i++) { scanf("%d",ptr+i); sum+= *(ptr+i); } printf("sum=%d",sum); free(ptr); getch(); }