Heap


 * This article discuses the heap from which memory is allocated. For the data structure, see Heap (data structure).

The heap is a pool of memory, typically allocated from the operating system, whence dynamic allocations of new storage come. Although the C++ standard does not discuss the existance of a heap, all implementations have a heap of some form.

The heap is used for all dynamic memory allocation: malloc, new, etc. Some implementations might have multiple heaps, with special functions to select from which heap memory should be allocated.

The heap is the counterpart to the stack, which is used for small, function-local storage allocation.

Heap fragmentation
Heap fragmentation occurs when there are many small blocks of free memory in the heap, but not enough to satisfy new allocation requests. This results in wasted memory, as more memory must be requested from the OS for these allocations, and impedes performance with additional overhead. In programs which allocate many objects of fixed size, memory pools can be used instead of the heap, to provide fast, fragmentation-free allocation of particular objects.