By the end of this lesson, cadets will be able to:
Complete before class. The instructor notes and diagram below are the primary reading; the optional source goes deeper.
| Source | Sections | Why |
|---|---|---|
| Instructor notes (Section 4 below, with diagram) | All | The five segments and what lands in each. This is the required reading. |
| Beej-C | Pointers and Manual Memory Allocation (review) | Optional refresher on the heap and pointers from earlier blocks. |
malloc and free). If either feels rusty, skim your notes from those lessons before class.You have spent the semester declaring variables, calling functions, and allocating memory with malloc. Each of those things ends up in a specific region of memory, and the region is not arbitrary. Today you learn the map of a running C program and the two questions that decide where anything lives.
The Lesson 38 deck walks the memory map from top to bottom, places examples in each segment, and closes by looking ahead to your team presentations. The slides are the spine of the in-class lecture; the memory-map walkthrough is where you practice placing things yourself.
Open Lesson 38 slides → replace href with your SharePoint link
When your program runs, the operating system gives it an address space: a range of memory addresses laid out from high to low. That space is divided into regions, each with its own job and its own rules. High addresses sit at the top; the lowest addresses at the bottom. This is the single picture to carry out of this lesson.
The five segments of a C process, with the stack and heap growing toward each other through the unused middle.
Read the map from the bottom up, because that is roughly the order in which things are decided, from fixed-at-compile-time to decided-at-run-time.
| Segment | What lives here | Lifetime |
|---|---|---|
| Text | Your compiled code, plus string literals and other read-only constants. | Whole run. Read-only; writing here crashes. |
| Data | Globals and statics that have an initial value. | Whole run. |
| BSS | Globals and statics with no initializer. Zeroed at startup. | Whole run. Costs no bytes in the executable file. |
| Heap | Memory you request at run time with malloc, calloc, realloc. |
From your malloc until your free. Your responsibility. |
| Stack | Function parameters, local variables, and return addresses. One frame per active call. | Reclaimed automatically when the function returns. |
The pointer trap. A line like animal_t *p = malloc(sizeof(animal_t)); creates two objects in two different segments. The pointer variable p lives on the stack; the block it points to lives on the heap. When the function returns, the stack reclaims p automatically, but the heap block stays allocated until you free it. Forgetting that is exactly the leak that valgrind reports.
You do not need to memorize a table of cases. For any variable, two questions place it:
static, lives for the whole run and goes in data or BSS. Anything from malloc goes on the heap.Notice that scope, not syntax, decides the home. The exact same line int n = 5; goes on the stack inside a function and into the data segment at file scope. That is the single most useful idea in this lesson, and the walkthrough drills it directly.
The address space in the diagram is virtual. Every running program sees the same clean layout starting from low addresses, as if it owned the whole machine. It does not. The operating system maps each program's virtual addresses onto whatever physical memory is actually free, and keeps each program's space private from the others.
Two practical consequences follow. First, the addresses you print are virtual, so the same program run twice can report different numbers (modern systems also randomize the layout on purpose for security). Second, one program cannot read or corrupt another's memory by accident, because they do not share an address space. You do not need the page-table details for this course. You need the mental model: each process gets its own private map, and the diagram is that map.
This lesson does not introduce a new style rule; it reinforces one you already follow. The heap discipline in §8.5 (every allocation has a matching free; no leaks) is exactly what the memory map makes concrete. The stack reclaims its frames for you, but the heap is yours to manage. The capstone lab is graded in part on a clean valgrind run, which is the standard's no-leak rule made measurable. The full standard lives in the course Resources section.
Two things to do in class. Use the checklist to track your progress.
Twelve tasks across four sections. Each task names a scope (file scope, or inside main). Decide which segment the object lands in before you press Enter, then type the declaration and watch the right pane.
Declare or allocate what each task asks for. Before you press enter, decide which segment it lands in — then watch the map.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 38 reflection.
shelter.c, the exact compiler or valgrind message you do not understand, and the line it points at. Specific questions get unstuck quickly.