kids encyclopedia robot

C dynamic memory allocation facts for kids

Kids Encyclopedia Facts

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.

Function Description
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.

Example code

int *array = malloc(10 * sizeof(int));
kids search engine
C dynamic memory allocation Facts for Kids. Kiddle Encyclopedia.