0Pricing
Swift Academy · Lesson

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

  1. Stack vs Heap: Where Values Live
  2. Mutation, Sharing and Unexpected Aliasing
  3. Mixed Value/Reference Graphs
  4. Designing APIs for Value Semantics
← Back to Swift Academy