Dynamic Memory Allocation in C Using Malloc
Dynamic Memory Allocation in C Using Malloc
realloc()
Syntax:
ptr = (cast-type*)calloc(n, element-size);
Syntax:
free(ptr);
Syntax:
ptr = realloc(ptr, newSize);
Difference between malloc and calloc()
The name malloc and calloc() allocates memory dynamically from the heap
segment.
Initialization: malloc() allocates memory block of given size (in bytes) and
returns a pointer to the beginning of the block. malloc() doesn’t initialize the
allocated memory. If we try to access the content of memory block (before
initializing) then we’ll get segmentation fault error(or maybe garbage
values).
Initialization: calloc() allocates the memory and also initializes the
allocated memory block to zero. If we try to access the content of these
blocks then we’ll get 0.
Arguments: malloc() takes single argument where ass calloc() takes two
arguments.