Stack vs heap.
A neat stack of trays you add and remove from the top, versus a big warehouse where you ask for space and must give it back.
- New call — tray on top.1
The stack is a pile of cafeteria trays: each call drops a frame on top.
- Done — off it comes.2
When a call returns, its tray pops off and that space is instantly free again.
- Stack too deep — overflow!3
It’s fast because allocating is just nudging a pointer up. But the pile is small.
- Space for this? Here’s the slot.4
The heap is a warehouse: ask for room and you get a slip telling you where it sits.
- Follow the slip to the shelf.5
You reach the data through that reference. It can outlive the call that made it.
- Give it back, or it leaks.6
But borrowed space must come back — freed by hand, or swept by a garbage collector.
Semicolony semicolony.dev/eli5/stack-vs-heap/comic