Stack vs Heap: Where Values Live
How structs live on the stack and classes on the heap, and the performance implications.
Welcome
Understanding where Swift values live in memory — the stack vs the heap — is essential for writing performant, predictable code.
The Stack
The stack is a contiguous block of memory managed automatically:
• Function locals and parameters live here
• Allocation is O(1) — just move a pointer
• Automatically freed when the scope exits
• Thread-local — each thread has its own stack
All lessons in this course
- Stack vs Heap: Where Values Live
- Mutation, Sharing and Unexpected Aliasing
- Mixed Value/Reference Graphs
- Designing APIs for Value Semantics