C dynamic memory allocation facts for kids
In the C programming language, dynamic memory allocation refers to allocating memory during a program's run time. Dynamically allocated memory is obtained from a storage pool called a heap. A group of functions in the C standard library are typically used for dynamic memory allocation.
Overview of functions
C dynamic memory allocation functions are defined in stdlib.h header.
|malloc||allocates the specified number of bytes|
|realloc||increases or decreases the size of the specified block of memory, moving it if necessary|
|calloc||allocates the specified number of bytes and initializes them to zero|
|free||releases the specified block of memory back to the system|
Differences between malloc and calloc
- malloc takes a single argument (the amount of memory to allocate in bytes), while calloc needs two arguments (the number of variables to allocate in memory, and the size in bytes of a single variable).
- malloc does not initialize the memory allocated, while calloc guarantees that all bytes of the allocated memory block have been initialized to 0.
int *array = malloc(10 * sizeof(int)); free(array);
C dynamic memory allocation Facts for Kids. Kiddle Encyclopedia.