Colour Skin
Phantom
Matrix
Hellfire
Abyss
Reactor
Caveman
Win 95
Aero
🧠 BrainStuffer
The Stack
Push, pop, frames & memory — visually explained
PUSH
sub rsp, 8  •  mov [rsp], val
Decreases RSP by 8 (makes space), then writes the value there. Stack grows down in memory.
POP
mov reg, [rsp]  •  add rsp, 8
Reads the value at RSP into a register, then increases RSP by 8 (releases the slot).
🏛
LIFO
Last-In, First-Out
Like a stack of plates — you can only add or remove from the top. Always O(1): one register change + one memory access.

Try it — Interactive Stack

Stack — top of memory (low addr) at top
stack is empty — push something!
RSP 0x7ffd_5000 stack pointer (top of stack)
Operation Log
What you are seeing
The top item is what RSP points to right now. Each push decreases the address by 8; each pop increases it. Notice the address in the log changes by exactly 8 bytes each time.
Text (.text)
Your compiled machine code. Read-only.
Heap
malloc/new live here. Grows upward.
Stack
Local vars, return addrs. Grows downward.
Kernel
OS memory. User code cannot touch this.
Virtual Address Space (x86-64, high→low)
Kernel
OS code & data. Inaccessible from user space.
0xffff800000000000+
Stack grows DOWN — RSP decreases on each push
Stack
Local variables, saved registers, return addresses. Each thread gets its own. Default ~8 MB limit.
~0x7fff_xxxx_xxxx
mmap
Shared libraries (.so/.dll), anonymous mappings.
varies (ASLR)
Heap grows UP — brk pointer increases on malloc
Heap
malloc() / new allocations. Managed by the allocator. Lifetime is manual.
varies (ASLR)
BSS / Data
Global and static variables. BSS = uninitialised (zeroed by OS), Data = initialised.
fixed at link time
Text
Executable machine code. Read-only & NX-protected — cannot be written at runtime.
~0x400000
Null page
Unmapped on purpose. Dereferencing a null pointer faults here.
0x0000
Zoom: one stack frame
Inside a function call — add(a=3, b=8)
► RSP → top of frame (lowest address)
rsp+0x00
local_b = 8local var
rsp+0x08
local_a = 3local var
► RBP → frame base (anchor)
rsp+0x10
saved RBPcaller's frame base
rsp+0x18
return address⚠ jump here on RET
rsp+0x20
arg a = 3passed by caller
What each colour means
Local vars — space the function reserved for its own data
Saved RBP — remembers where the caller's frame started
Return address — where to jump when the function returns
Arguments — values the caller passed in (extra ones go on stack)
Local variables
int x, char buf[] — live here until return
Saved RBP
Caller's frame base — restored on return
Return address
RET pops this into RIP — jumps back to caller

Step Through: main() calls add(3, 5)

Step 1 / 7
Stack (current frame at top)

Recursion & the Call Stack

Every function call adds a frame. A recursive function calls itself, stacking frame after frame until the base case. Walk through factorial(4) — watch the frames pile up, then unwind in reverse order as values are returned (LIFO at work).

Step 0 / 9
Ready
Press "Next step" to start calling factorial(4).

⚠ Stack Overflow

What if recursion never hits a base case? Frames pile up forever until the OS kills the process with SIGSEGV (default stack limit ~8 MB on Linux).

depth: 0 / 100

Stack vs Heap — Quick Reference

▲ Stack
Allocation
Automatic — compiler emits sub rsp, N
Free
Automatic — RSP restored on return
Speed
O(1) — single register move
Max size
~1–8 MB per thread (OS default)
Lifetime
Until the function returns
Threads
Each thread gets its own stack — safe
Size known?
Must be fixed at compile time
Danger
Stack overflow (too deep recursion)
◆ Heap
Allocation
Manual — malloc() / new
Free
Manual — free() / delete (or GC)
Speed
Slower — allocator searches free list
Max size
Limited only by virtual memory
Lifetime
Until you explicitly free it
Threads
Shared — allocator must lock
Size known?
Can be determined at runtime
Danger
Memory leak, use-after-free, heap overflow

In Code

Stack (C)
void foo() { // both live on the stack int x = 42; char buf[256]; process(buf); } // freed automatically
Heap (C)
void bar() { char *p = malloc(256); process(p); free(p); // you MUST do this // forget -> memory leak! }
Stack (C++)
std::string s = "hello"; // s is on the stack // ~string() called auto // when scope exits (RAII)
Heap (C++ smart ptr)
auto p = std::make_unique <Widget>(args); // heap, but RAII: // unique_ptr frees it // automatically

Quiz — Test Your Stack Knowledge