Select Page

Functions for dynamic memory allocation in C

by 20.03.2023Dynamic Memory Management in C

There are several memory allocation functions to manage dynamic memory. If they are available or not may depend on the system. However the following four functions are available on most systems in the header file stdlib.h.

  • malloc: allocates memory from the heap,
  • realloc: reallocates memory,
  • calloc: allocates memory from the heap and place there zeros,
  • free: returns a block of memory to the heap (releases the memory).

Important is understand the dynamic memory is allocated from the heap. In this case there is no guarantee regarding the order of the memory and the continuity of memory that is allocated.

The allocated memory is aligned according to the pointer’s data type. For example, a four byte integer will be allocated on an address boundary that is evenly divisible by four and the returned address by heap manager will contain the lowest byte’s address.

SOURCES:
[1] Understanding and Using C Pointers by Richard Reese (O’Reilly). Copyright 2013 Richard Reese, Ph.D. 978-1-449-34418-4