Skip to main content

Section 22.3 The Heap

When the stack does not have enough room for what we wish to store, or when we wish to allocate some memory and have it persist after the function that allocates it returns, we need to place the memory somewhere else. That somewhere else is the heap (also known as the free store).
The memory space for a program is broken up into different segments that each have a different purpose. One segment is used to store the actual code of the program. Another segment is used to store global variables. A third segment is used for the stack. Those segments are all fixed as the program starts running. That is why you can have a stack overflow - the stack β€œgrows” from the β€œbottom” (highest address) towards its lowest address and if the β€œtop” ever grows past that lowest address, it has overflowed.
The heap is another such segment. It is not organized in a strict manner like the stack. Instead, it is a large pool of memory that can be allocated and deallocated as needed. (The name β€œheap” should evoke the idea of a messy pile of things from which we can freely add or remove things.)
Memory segments. The stack is managed by the compiler and is small. The heap is managed by the programmer and is much larger.
Figure 22.3.1. Memory segments. This image is not to scale. The heap is typically thousands of times larger than the stack.
Table 22.3.2. Stack vs Heap
Stack Heap
Managed by
Compiler
Programmer (Possibly with help from a memory management library or garbage collector)
Size
Typically small (e.g., a few MB)
Typically large (e.g., many GBs)
Organization
Organized in a last-in, first-out (LIFO) stack.
Free-form. Memory can be allocated and deallocated in any order.

Note 22.3.1.

Not every computer system organizes memory into these segments, and the specifics can vary based on the architecture and operating system. In particular, small embedded systems may not have a traditional heap at all. But all typical desktop, server, and mobile systems use a memory management scheme like the one described here.

Checkpoint 22.3.1.

You have attempted of activities on this page.