Phase 2 — Memory & Pointers (M3)¶
This is the month that separates C dabblers from people who actually write C. You already survived syntax and toolchains in M1-M2; now you’re going to internalize the machine’s memory model until you can sketch it on a whiteboard for anyone asking. The stakes are real — every segfault in your future, every memory leak in a 3AM incident, every SIMD kernel you’ll write in Phase 6 traces back to whether the map in your head matches the map in the CPU.
The M3 Target¶
By July 31, 2026 you can:
Draw the memory diagram of any C program on a whiteboard — stack, heap, BSS, text, rodata, mmap regions — and correctly place every variable declared in the source.
Explain ownership for every
mallocin code you write — who allocates, who frees, and what convention the function signature implies.Diagnose the seven canonical memory bugs (heap overflow, use-after-free, double-free, leak, uninitialized read, stack overflow, alignment fault) both by symptom and by tool output (ASan, valgrind, gdb).
Write a working arena allocator in one sitting — bump + reset semantics, alignment-aware, roughly 100 lines. Ryan Fleury / Chris Wellons style.
Read gnarly declarations like
int (*(*fp)(int))[3]without cdecl.org — though you’ll still use cdecl.org because that’s not a weakness.
What Most People Skip and Regret¶
Ownership convention. Everyone learns malloc/free. Almost nobody sits down and decides, “in this codebase, functions that return a T* transfer ownership; functions that take a const T* borrow it.” Leaks in real C code are almost never accidental — they are the residue of an unclear ownership story. You’re going to fix that in yourself this month.
File Order¶
# |
File |
Why |
|---|---|---|
1 |
The map. Everything else references this. |
|
2 |
Why |
|
3 |
The API and its footguns. |
|
4 |
The convention that saves you. |
|
5 |
The seven bugs you’ll meet in production. |
|
6 |
Arena, pool, freelist — the modern C style. |
|
7 |
Function pointers and hairy declarations. |
|
8 |
Two deliverables that prove you got it. |
Exit Criteria (self-check, be honest)¶
You are done with M3 when all of the following are true. Not “mostly.” All.
You can start with a blank whiteboard and, given the C source of a 40-line program with globals,
staticlocals,malloc, and a recursive function, draw the memory layout correctly in under 5 minutes.ulimit -s— you know what it returns on your Mac (8176KB = ~8 MB default on macOS/Linux) and why declaringint arr[<phone_number_or_numberic_id_or_random_id_23>]insidemainsegfaults.You’ve shipped both projects in projects.md, and your arena allocator passes an ASan test suite with zero
ERROR: AddressSanitizermessages.You can explain to a friend why
reallocin a loop is almost always a bug, and what to do instead.You’ve read Chris Wellons’ Arena allocator tips and tricks at least twice — once for the mechanics, once for the philosophy.
You have a personal “ownership cheatsheet” in this folder documenting the convention you’ll use for the rest of the year (returns-owned vs. borrows-const-pointer vs. out-parameter-fills).
The Reading List (canonical)¶
Chris Wellons — nullprogram.com — start with the 2023 arena post, then read anything tagged
c. The single most useful modern C blog in existence, no exaggeration.Ryan Fleury — Untangling Lifetimes: The Arena Allocator — the article that got a generation of C programmers to abandon
malloc/freescatter.Ulrich Drepper — What Every Programmer Should Know About Memory (2007) — dated on NUMA and prefetching specifics (see StackOverflow’s line-by-line validity check), but sections 1-3 (RAM, cache) are still the best 40 pages on the subject. Skim §6-8 rather than study.
“How to Actually Write C” by zackoverflow — zackoverflow.dev/writing/how-to-actually-write-c — surprisingly good on the modern arena-first style.