Semicolony ELI5 · comic

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.

  1. New call — tray on top.
    THE STACK new
    1

    The stack is a pile of cafeteria trays: each call drops a frame on top.

  2. Done — off it comes.
    RETURN
    2

    When a call returns, its tray pops off and that space is instantly free again.

  3. Stack too deep — overflow!
    just bump a pointer overflow!
    3

    It’s fast because allocating is just nudging a pointer up. But the pile is small.

  4. Space for this? Here’s the slot.
    THE HEAP slip
    4

    The heap is a warehouse: ask for room and you get a slip telling you where it sits.

  5. Follow the slip to the shelf.
    ref on the stack data on the heap
    5

    You reach the data through that reference. It can outlive the call that made it.

  6. Give it back, or it leaks.
    free
    6

    But borrowed space must come back — freed by hand, or swept by a garbage collector.

A neat pile of trays you add and remove from the top, versus a warehouse you borrow space from and must return.
Semicolony semicolony.dev/eli5/stack-vs-heap/comic
← All ELI5 explainers