Memory management#Dynamic memory allocation

The dynamic memory, and heap ( or undocumented, heap ',' pile '), heap memory or free memory is a memory area which can be requested from the related to the duration of a program memory sections, and released again in any order. The release can be done manually or with the aid of an automatic garbage collection. A memory request from the heap is also called dynamic memory allocation. It serves the programs to the program through the code and fix the reserved data fields and the stack ( stack ) memory used also yet to have additional buffer memory.

For the purposes of developing dynamic memory management is a significant additional effort and is a frequent source of error, particularly for memory leaks. A typical error is, for example, that references are overwritten unintentionally on dynamically allocated memory and the originally referenced field can no longer be released. This can also lead to so-called hanging pointers.

Unlike the stack

The difference from the stack is that requested the stack memory sections must be freed in the reverse order in which they were requested. This limits the reuse is no longer required stack areas; also must give up before their return to the calling function is a function of their stack memory requirements. When stack is also called automatic memory requirement. The time needed for an automatic memory request at run-time is significantly less than the dynamic storage requirement of the rule. As for the stack usually only a small amount of memory is reserved, an unwanted program termination can occur due to stack overflow with constant use by very large or very many requirements.

Support for dynamic memory requirements in programming languages

Programming languages ​​support dynamic memory allocation in different ways. ISO -C, there are for example, the functions malloc (), calloc () and realloc (). With the function free () the memory is freed again.

In ISO -C , there is the ability to dynamically request memory with the help of new or re-enable using delete addition to those already assumed by C functions.

Memory management

Compared to stack the administration of the heap is complicated by the runtime environment. Storage areas should be possible at any time requested by the application and also released in any order. The size of the sectors, the time of the request or release are unpredictable.

Therefore the following, sometimes contradictory demands are made of the dynamic memory management:

  • High speed
  • Efficient memory usage
  • Low Administration

By the later release of a block can cause external fragmentation. An automatic garbage can combine the free space map again, so that larger free blocks are available.

For real-time applications, a dynamic memory management to be inappropriate, because both the term and the successful allocation must be guaranteed.


For the administration of free blocks various data structures are used:

  • Linked lists
  • Buddy systems
  • Heap - like structures
  • Search trees