C Programming · Interview question

Sketch the memory layout of a running C program.

A strong answer

From low to high addresses: .text (the executable machine code, usually read-only), .rodata (constants and string literals), .data (initialized globals and statics), .bss (zero-initialized globals and statics, which takes no space in the binary, just a size), then the heap growing upward from there, and the stack growing downward from the top of available memory. Stack and heap grow toward each other, and the gap between them is the free memory both draw from. On a desktop with virtual memory each region is effectively huge and isolated; on an MCU they share a few kilobytes of physical RAM, so a stack that grows too far collides with the heap or .bss. Knowing this layout explains why globals persist (they're in .data/.bss, not a frame), why string literals are read-only (.rodata), and why a stack overflow corrupts neighbors.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Stack vs Heap

Where your variables actually live: automatic stack storage vs manual heap allocation, and why returning a pointer to a local is the classic firmware crash.

More Stack vs Heap questions

Browse all 472 interview questions
Sketch the memory layout of a running C program. | EmbeddedPrep.io