Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. stack
10 min read

The Stack

The last lesson gave you the heap, the memory you manage by hand. This one is about the memory you don't — the stack, where your local variables live and where C keeps track of which function called which. You never allocate on it explicitly; it happens automatically with every function call. But seeing its shape explains three things at once: why local variables disappear when a function returns, why recursion works, and why a whole category of security bugs exists.

A quick note on orientation before we draw anything. GDB, and most debuggers, list memory with the lowest addresses at the top and the highest at the bottom, so every diagram here does the same. The important and slightly counterintuitive fact is that the stack grows downward — toward lower addresses. That means each new piece sits above the last in our pictures, at lower addresses, near the top.

The Call Stack

Every time you call a function, C sets aside a block of stack memory for it called a stack frame. That frame holds the function's local variables plus a little bookkeeping — most importantly, the return address: where execution should jump back to when the function finishes.

Frames stack up like a pile of plates. Calling a function pushes a new frame on top; returning pops it back off. This is why it is called a stack — it is strictly last-in, first-out. Here is what a single frame contains:

low high local variables $rsp saved base pointer $rbp return address caller's frame

Two CPU registers mark the current frame: $rsp (the stack pointer) tracks the very top, and $rbp (the base pointer) marks a fixed reference point inside it. Local variables are addressed relative to these. You met both briefly in the pointer lesson's debugger sessions; here they finally have a picture.

Let's make the stacking concrete. The program below has main call foo, which calls bar. We break inside bar and ask GDB for a backtrace — the list of frames currently on the stack:

frames.c
1#include <stdio.h>
2
3void bar(void)
4{
5    int
gdb
$ gcc -g frames.c -o framesgef➤ set output-radix 10gef➤ break barBreakpoint 1 at 0x1141: file frames.c, line 5.gef➤ runBreakpoint 1, bar () at frames.c:5gef➤ bt#0  bar () at frames.c:5#1  0x0000555555555178 in foo () at frames.c:12#2  0x000055555555518f in main () at frames.c:18gef➤ p $rsp$1 = (void *) 0x7fffffffe190gef➤ p $rbp$2 = (void *) 0x7fffffffe1a0

bt lists three frames: #0 is bar, the one we are stopped in; #1 is foo, which called it; #2 is main. That pile is exactly our diagram — bar newest, main oldest:

low high bar()local: z = 3#0 $rsp foo()local: y = 2#1 main()local: x = 1#2

The $rsp and $rbp values GDB printed are the real boundaries of bar's frame — $rsp lower, $rbp higher, the frame sitting between them, just as in the first picture.

Scope and Lifetime

Now the frame picture pays off. A local variable's lifetime is exactly the lifetime of its frame: it is born when the function is called and the frame is pushed, and it dies when the function returns and the frame is popped. Its scope — the part of the code that can see it — is the function it is declared in.

This is the real reason behind the dangling-pointer trap from the pointers lesson. When a function returns the address of one of its locals, it is handing out the address of a slot in a frame that no longer exists:

c
1int *broken(void)
2{
3    int local = 5;
4    return &

The moment broken returns, its frame is popped, and that memory is free for the next function call to overwrite. The address is still a valid-looking number, but what it points to is gone. When you need data to outlive the function that creates it, that is precisely what the heap is for.

Why You Can't Allocate Variable-Size Stack Memory

Stack frames are fast because the compiler lays them out ahead of time. It decides at compile time exactly how big each frame is and where every local sits within it — x at this offset from $rbp, z at that one. Those offsets are baked into the compiled instructions.

That speed comes with a restriction: the size has to be known when the program is compiled, not when it runs. You cannot cleanly ask the stack for "however many bytes I turn out to need." This does not compile into something portable or safe:

c
1int n = get_count(); // only known at runtime
2int buf[n];          // size isn't fixed — fragile, and easy to blow the stack

When the amount is only known at runtime, you use the heap instead, where asking for a runtime size is the whole point:

c
1int *buf = malloc(n * sizeof(int)); // the portable, safe way
2free(buf);

This is the practical dividing line between the two: fixed and known in advance goes on the stack; variable or long-lived goes on the heap.

Recursion and the Stack

Because every call gets its own frame, a function that calls itself simply gets a new frame each time — its locals do not clash, because each call's variables live in a separate frame. That is what makes recursion work. Take the classic factorial:

rec.c
1#include <stdio.h>
2
3int factorial(int n)
4{
5    if

If we break at the base case — the moment n reaches 1 — the stack holds a separate frame for every call still in progress:

gdb
$ gcc -g rec.c -o recgef➤ set output-radix 10gef➤ break 6Breakpoint 1 at 0x114a: file rec.c, line 6.gef➤ runBreakpoint 1, factorial (n=1) at rec.c:6gef➤ bt#0  factorial (n=1) at rec.c:6#1  0x000055555555515e in factorial (n=2) at rec.c:8#2  0x000055555555515e in factorial (n=3) at rec.c:8#3  0x0000555555555172 in main () at rec.c:13

Each factorial has its own n, frozen at the value it was called with, waiting for the call above it to return so it can finish its own multiplication. The backtrace is the picture:

low high factorial(n = 1)#0 factorial(n = 2)#1 factorial(n = 3)#2 main()#3

As each call returns, its frame pops and the one below it continues — factorial(1) returns 1, then factorial(2) computes 2 * 1, then factorial(3) computes 3 * 2, and main finally prints 6.

This also shows recursion's danger. Every call consumes a frame, and the stack is finite. Recursion that goes too deep — or never reaches its base case — keeps pushing frames until it runs off the end of the stack. That crash is a stack overflow, and it is exactly where this course's namesake, the stack, meets the world of exploitation.

With the stack and the heap both in hand, you now know where every piece of data in a C program lives. Next we build the things you store in that memory, starting with Arrays.

z
=
3
;
6 printf("%d\n", z);
7}
8
9void foo(void)
10{
11 int y = 2;
12 bar();
13}
14
15int main(void)
16{
17 int x = 1;
18 foo();
19 return 0;
20}
local
;
// &local points into broken's frame...
5}
(
n
<=
1
)
6 return 1;
7
8 return n * factorial(n - 1);
9}
10
11int main(void)
12{
13 printf("%d\n", factorial(3));
14 return 0;
15}