Operating systems / Stack vs heap memory

Stack vs heap memory

Where a program keeps its data: the stack that grows and shrinks with every function call, and the heap you ask for by hand. Followed slot by slot, with real addresses.

Intermediate7 steps~7 min
Frame — one function call’s slots on the stack
Stack pointer — marks where the stack currently ends
Heap block — memory reserved on request
// THE SHORT VERSION

In 30 seconds

A program stores data in two regions. The stack holds the local variables of running functions. Each call adds a frame and each return removes it, automatically and almost for free.

The heap is a pool the program requests blocks from when it needs data of any size that outlives a function call. Asking is slower, and each block stays reserved until it is released.

Each fails in its own way: calls nested too deep overflow the stack, and heap blocks that are never released leak.

Stack vs heap memory, step by step

STEPS01 / 07

Simplified: real compilers keep many values in registers and pad frames, and real allocators add bookkeeping bytes to each block. The stack grows downward on most processors, but not on all.

STEP 01 / 07 Two places for data
THE MATH
stack: automatic, per call heap: on request, any size
  1. Step 1: Two places for data. A running program keeps its data in two regions of memory. The stack holds the local variables of the functions currently running and looks after itself. The heap is a large pool from which the program asks for blocks of any size, whenever it wants, and which it must hand back itself.
  2. Step 2: A call pushes a frame. main is running; its variables a and r sit in its stack frame. When main calls square(3), a new frame is pushed below it: where to return to, the argument n and the local s. Making that room costs one subtraction: the stack pointer (SP), which marks the end of the stack, moves down by 24 bytes.
  3. Step 3: Return pops it. square hands back 9 and returns. Its frame is released by moving the stack pointer back up by 24 bytes: nothing is searched for, nothing is erased. That is why stack memory is so fast, and why a local variable only lives as long as its function call. Keeping the address of n or s after this point is a bug.
  4. Step 4: The heap: ask for memory. Some data cannot live in a frame: its size is only known while running, or it must outlast the function that creates it. It goes on the heap. Here load requests 12 bytes; the allocator finds a free block at 0x5000 and returns its address, kept in the local variable p. Finding a block takes more work than moving SP.
  5. Step 5: Lifetime is up to you. A heap block stays reserved until the program frees it, however many functions return in the meantime. That is its strength and its danger. Here load returns without calling free(p): the pointer vanishes with the frame, the block stays reserved, and nothing knows its address any more. That is a memory leak.
  6. Step 6: Stack overflow. The stack is a fixed-size region, commonly 1 to 8 MB per thread. A function that keeps calling itself pushes frame after frame and never pops one. With 24-byte frames and an 8 MB stack, 349 525 calls fit; the next one runs past the end. The system stops the program: a stack overflow.
  7. Step 7: Side by side. The stack is the default for small, short-lived data: it costs almost nothing and cleans up after itself, but it is limited in size and its contents die with the call. The heap takes data of any size for as long as needed, but allocation is slower and the block must be released: a job many languages give to a garbage collector.
VALUES SO FAR
stackautomaticregion
heapon requestregion
SP····in call
SP····returned
r····result
p····pointer
block····heap
leak····lost
fits····limit

Spotted a mistake on this page?