Dynamic Memory Allocation
Dynamic Memory Allocation
calloc() malloc()
calloc() initializes the allocated memory malloc() initializes the allocated memory
with 0 value. with garbage values.
Syntax : Syntax :
(cast_type *)calloc(blocks , size_of_block); (cast_type *)malloc(Size_in_bytes);
int* array;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
array = (int*) calloc(n,sizeof(int));
Dynamic Array
Steps for creating 2d Array using Pointer.
• Create a pointer to pointer and allocate the
memory for the row using malloc().
1 for(i = 0; i < nrows; i++)
2 {
3 piBuffer[i] = malloc( ncolumns * sizeof(int));
4 }
Dynamic Arrays
• Allocate memory for each row columns using
the malloc().