Call Stack & Heap
Prev: Compilation, Interpretation & JIT | Up: memory_and_execution | Next: Pointers, References & Indirection
Rel: Pointers, References & Indirection, Memory Management Models
Ref: Wikipedia — Call Stack
Tags: #public #prog #programming #memory #stack #heap
Contents
Process Virtual Memory Layout
High addresses
┌─────────────────────────┐
│ Call Stack (grows down) │ <- Function frames, local fixed-size values
│ ↓ │
│ (free space) │
│ ↑ │
│ Heap (grows up) │ <- Dynamically sized / long-lived allocations
├─────────────────────────┤
│ Static / Global Data │ <- Global & static variables (.data / .bss)
├─────────────────────────┤
│ Text / Code Segment │ <- Read-only executable machine instructions
└─────────────────────────┘
Low addresses
Stack vs. Heap Trade-Offs
| Property | Call Stack | Heap |
|---|---|---|
| Allocation speed | Single pointer bump ($O(1)$, nanoseconds) | Allocator search + metadata bookkeeping |
| Lifetime | Strictly tied to function return (LIFO) | Arbitrary; outlives creating function |
| Size | Small fixed limit (typically 1–8 MiB/thread) | Bounded by system virtual memory |
| CPU Cache | Excellent spatial locality | Prone to fragmentation & cache misses |
Linked from
- tuts/c/compilation_and_internals/memory_layout_and_abi
- tuts/c/pointers_and_memory
- tuts/programming/foundations/functions_and_recursion
- tuts/programming/foundations/state_variables_and_scope
- tuts/programming/memory_and_execution
- tuts/programming/memory_and_execution/call_stack_and_heap
- tuts/programming/memory_and_execution/compilation_interpretation_and_jit
- tuts/programming/memory_and_execution/memory_management_models
- tuts/programming/memory_and_execution/pointers_references_and_indirection
- tuts/programming/paradigms/imperative_and_procedural
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes
Related notes
- tuts/programming/memory_and_execution Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/programming/memory_and_execution/compilation_interpretation_and_jit Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/programming/memory_and_execution/memory_management_models Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/programming/memory_and_execution/pointers_references_and_indirection Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/c/compilation_and_internals/memory_layout_and_abi links to this page; shared tags: #memory, #prog
- tuts/c/pointers_and_memory links to this page; shared tags: #memory, #prog
- tuts/programming/foundations/functions_and_recursion links to this page; shared tags: #prog, #programming
- tuts/programming/foundations/state_variables_and_scope links to this page; shared tags: #prog, #programming
- tuts/programming/paradigms/imperative_and_procedural links to this page; shared tags: #prog, #programming
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes links to this page; shared tags: #memory, #prog