CC Explainer
C · 5 · Systems C23 / 35 · 66%

Memory Layout of a Process

Text, data, BSS, heap, stack — where every variable lives.

shortcuts: ← prev · → next · M mark
1

The 5 Segments

The OS loader carves your process into distinct regions.

Stackgrows down ↓Heapgrows up ↑BSSuninitialisedDatainitialised globalsTextcode / read-only
process address space
2

Where does X live?

Storage class and declaration site decide the segment.

Example
example
int    g;              // BSS  (zero-initialised)
int    g2 = 5;         // DATA (initialised)
static int s;          // BSS  (file scope only)
const char *msg = "hi";// pointer in DATA, "hi" in read-only TEXT

int main(void) {
    int   local;        // STACK
    int  *heap = malloc(4);  // *heap is on HEAP
}
3

Stack Frames

Each function call pushes a frame; return pops it.

main()ret, args, localsfoo(int)ret, args, localsbar()ret, args, localstop ↑
call stack grows toward lower addresses