Skip to main content
Be Bitwise
CC Basics & Memory

The C Memory Model

If you internalise one thing from this track, make it the C memory model. Segfaults, leaks, buffer overflows, every painful bug you'll meet eventually traces back to memory.

The Four Regions

When a C program runs, the OS lays its memory out in four chunks:

  1. Text (Code) — your compiled instructions, mapped read-only and executable.
  2. Data/BSS — globals and statics. Initialised ones land in Data; the uninitialised ones go to BSS, which is just zero-filled at load time.
  3. Stack — locals and function call frames. Grows and shrinks on its own as calls come and go.
  4. Heap — the freeform region you hand-manage with malloc and free.

Process Memory Layout

0xFFFF...0x0000...StackgrowsFree SpaceHeapgrowsBSSDataText

You can watch the layout for yourself by printing where things live. Before you run it, guess which addresses will sit close together and which will be far away from the rest.

Try itRun it twice: some addresses move between runs (address randomisation), but the grouping doesn't.
C (GCC 9.2.0)

On Linux you'll typically see code, the literal and the globals packed together at the low end, the heap a little above them, and the stack way up near the top of the address space. Casting a function pointer to void * isn't strictly portable C, but GCC on Linux does exactly what you'd hope.

Stack Memory

Every function call pushes a fresh stack frame. Inside that frame:

  • The locals declared inside the function.
  • The return address (where execution resumes when the function ends).
  • Saved register values the function had to clobber.

Return from the function and the frame pops right back off. That's why locals "disappear" the moment a function finishes — their memory is just reclaimed.

c
void foo() {
int x = 10; // x lives on foo's stack frame
} // x's memory is reclaimed here

Step through three nested calls below. Each call stacks a new frame on top of its caller's, and each return throws the top frame away, locals and all. The addresses are illustrative, but notice that each new frame's saved rbp is lower than the last: on x86-64 the stack grows towards lower addresses.

Call Stack Animator

1000ms
Pseudocode
1void bar() {
2 int z = 30;
3}
4 
5void foo() {
6 int y = 20;
7 bar();
8}
9 
10int main() {
11 int x = 10;
12 foo();
13 return 0;
14}
Stack (grows downward)
Stack is empty. Press Step or Call to begin.
Step 0 / 6
Warning

Don't return a pointer to a local. The frame it lives in is gone the instant the function returns, and the pointer now aims at memory that the next call will happily reuse. Classic C footgun.

Heap Memory

The heap is where dynamic allocation lives. Stack memory comes and goes with function calls; heap memory hangs around until you free it yourself.

c
int *p = malloc(sizeof(int)); // allocate on heap
*p = 42; // use it
free(p); // release it

The heap is roomier and far more flexible than the stack, but the bookkeeping is on you:

  • You free every allocation. No exceptions.
  • You stop touching memory once it's freed.
  • You never free the same block twice.

It costs time too. Each malloc has to find a free block of the right size, and a long-running program that allocates and frees blocks of mixed sizes can fragment the heap: plenty of free memory in total, but scattered in gaps too small to use.

Why This Matters

In a GC'd language you barely notice memory regions exist. The runtime quietly shuffles things around for you. In C, every allocation is a deliberate call. That's where C's reputation for raw speed comes from, and also where its reputation for spectacular bugs comes from. Same coin, different sides.

Next up: malloc and free, the two functions that gate your access to the heap.