Stack vs Heap, Simply
Where values live and why it matters.
Stack vs Heap, Simply is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Places for Data
Every value in your program lives somewhere in memory. Mojo uses two regions: the stack and the heap. 🧠
The Stack
The stack is a fast, orderly pile of memory. Local variables go here, and they vanish automatically when a function returns.
Why the Stack Is Fast
The stack is fast because its size is known and it grows and shrinks in a simple last-in, first-out order, like a stack of plates.
The Heap
The heap is a larger, flexible pool of memory. You use it when a value's size is not known until the program is actually running.
When You Need the Heap
A list that grows, a long string, or anything sized at runtime lives on the heap, since the stack needs fixed, known sizes.
Small, Fixed Values
A simple Int or Float has a fixed, tiny size, so Mojo can keep it right on the stack for the fastest possible access.
var count: Int = 7Growing Data
A List can grow without limit, so its elements live on the heap while a small handle to them sits on the stack.
var nums = List[Int](1, 2, 3)Speed Trade-Off
The heap is more flexible but slower to manage than the stack. Reaching for heap memory has a real cost you should respect.
Automatic Cleanup
Stack values clean themselves up at the end of their scope. Mojo also tracks heap values so they are freed at the right time for you.
You Rarely Choose by Hand
Most of the time Mojo decides stack or heap based on the type. Knowing the difference simply helps you reason about speed.
A Mental Model
Picture the stack as a quick scratchpad and the heap as deep storage. Fast and small versus large and flexible.
Quick Check
Let us see where a growable value lives.
Recap
The stack is fast and fixed-size; the heap is flexible for runtime-sized data. Knowing both helps you reason about speed. 🎯
Frequently asked questions
Is the “Stack vs Heap, Simply” lesson free?
Yes — the full text of “Stack vs Heap, Simply” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Stack vs Heap, Simply”?
Where values live and why it matters. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Stack vs Heap, Simply” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Stack vs Heap, Simply
- Pointers Without Fear
- Owning Heap Buffers
- Avoiding Common Memory Bugs