Dynamic storage

from Wikipedia, the free encyclopedia

The dynamic memory , also heap , heap memory or free memory is a memory area from which related memory sections can be requested during the runtime of a program and released again in any order. The share can be done manually or with the help of an automatic garbage collector. A memory request from the heap is also called a dynamic memory request. It is used by the programs to have additional buffer memory available beyond the memory occupied by the program code itself and the permanently reserved data fields and the stack (stack memory) .

For application development, dynamic memory management means considerable additional effort and is a frequent source of errors, especially for memory leaks . A typical error is, for example, that references to dynamically allocated memory are unintentionally overwritten and the originally referenced area can no longer be released. Conversely, references to memory that have already been released can also remain. Such references are known as hanging pointers .

Difference to the stack

The difference to the stack is that memory sections requested from the stack have to be released in the reverse order in which they were requested. This restricts the reuse of stack areas that are no longer required; likewise, a function must relinquish its stack memory claims before returning to the calling function. In the case of the stack, one speaks of automatic memory request . The time required for an automatic memory request at runtime is usually significantly less than for a dynamic memory request. Since usually only a small memory area is reserved for the stack, an undesired program termination due to a stack overflow can occur in the event of intensive use due to very large or very many requests .

Support of dynamic memory requirements in programming languages

Programming languages ​​support dynamic memory requirements in different ways. In ISO- C there are, for example, the functions malloc () , calloc () and realloc () . The memory is then released again with the free () function .

In ISO- C ++ , in addition to the functions already taken over from C, there is the possibility of dynamically requesting memory with the help of new or releasing it again with delete .

Memory management

In comparison to the stack, the administration of the heap by the runtime environment is more complex. Memory areas should be requested by the application at any time and released again in any order. The size of the areas and the time of the request or release are unpredictable.

The following, partly contradicting requirements are therefore placed on dynamic memory management:

  • High speed
  • Efficient use of memory
  • Little administrative effort

Releasing a block later can result in external fragmentation . An automatic garbage collection can reunite the free memory areas so that larger free blocks are available.

Dynamic memory management can be unsuitable for real-time applications , since both the runtime and the successful allocation must be guaranteed.


Various data structures are used to manage the free blocks:

See also

Web links