5.1.15C Programming

Memory layout — text, data, BSS, heap, stack segments

2,233 words10 min readdifficulty · medium5 backlinks

WHAT are the segments?

Figure — Memory layout — text, data, BSS, heap, stack segments

WHY does each segment exist? (first-principles reasoning)


HOW is the address space arranged?


WORKED EXAMPLES — where does each variable live?


Common mistakes (Steel-man + fix)


Active recall

Recall Try before peeking
  • Which segment is read-only? Text.
  • int x; global lives where? BSS.
  • int x = 9; global lives where? Data.
  • Does BSS occupy disk space? No (only a size record).
  • Which way does the heap grow? Up (higher addresses).
  • Which way does the stack grow? Down (lower addresses).
  • Where does malloc's block live? Heap.
  • Where does a function's local int live? Stack.
Recall Feynman: explain to a 12-year-old

Imagine your program gets a big bookshelf. The bottom shelf holds the instruction manual — you can read it but never scribble on it (Text). The next shelves hold toys you already filled with stuff (Data) and empty boxes labeled "fill with zeros" (BSS) — the empty boxes don't need to be packed in the moving truck, you just write "10 empty boxes" on a sticky note. In the big middle space, the toy pile grows up from the floor every time you ask for a new toy (Heap = malloc), and a stack of plates grows down from the ceiling every time you start a new chore and place a plate (Stack = function calls). When the chore is done, you take the plate back automatically. If the toy pile and plate stack ever crash into each other — you're out of room!


Connections

  • malloc, calloc, realloc and free — how the heap is managed.
  • Stack frames and the calling convention — what's inside a stack push.
  • Static and global variables — storage duration — Data vs BSS rules.
  • Pointers and dangling pointers — heap lifetime bugs.
  • Virtual memory and paging — how segments map to physical RAM.
  • Buffer overflow and stack smashing — security side of the stack.
Which segment stores compiled machine instructions and is read-only?
The text (code) segment.
Where do initialized non-zero global/static variables live?
The data segment.
Where do uninitialized or zero-initialized globals/statics live?
The BSS segment.
Why does BSS take no space in the executable file?
It stores only a size to reserve; the OS zero-fills it at load time, so the all-zero contents need not be saved on disk.
What is the default value of an uninitialized GLOBAL variable in C?
Zero (it goes to BSS, guaranteed zero-initialized).
What is the value of an uninitialized LOCAL (auto) variable?
Indeterminate/garbage — it's on the stack and not auto-zeroed.
Which direction does the heap grow?
Toward higher addresses (program break moves up via brk/sbrk).
Which direction does the stack grow?
Toward lower addresses (stack pointer decreases on push).
When you call malloc inside a function, where is the returned block, and where is the pointer?
The block is on the heap; the pointer variable is on the stack.
What tracks the top of the heap?
The program break, adjusted by brk/sbrk.
What tracks the top of the stack?
The stack pointer register (e.g. rsp).
What causes a stack overflow vs heap exhaustion?
Stack overflow = too-deep recursion/huge locals (SP past limit); heap exhaustion = malloc returns NULL.

Concept Map

sliced into

low addr, RO

non-zero globals

zero/uninit globals

runtime alloc

call frames

shared across processes

saves disk space

grows up, tracked by

grows down, tracked by

share gap, collide

share gap, collide

manual free

automatic

Virtual memory chunk

Five segments

Text - machine code

Data segment

BSS segment

Heap

Stack

One read-only copy

Zero-filled at load

Program break brk/sbrk

Stack pointer rsp

Overflow

malloc/free by you

Freed on return

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Jab tum ek C program run karte ho, OS usko ek private virtual memory deta hai, aur woh memory alag-alag segments mein bant ti hai. Sabse neeche Text segment hota hai — yeh tumhara compiled code hai, read-only, taaki koi galti se ya virus tumhare instructions ko overwrite na kar de. Uske upar Data segment hota hai jisme woh global/static variables aate hain jinhe tumne non-zero value se initialize kiya (jaise int x = 5;). Phir BSS aata hai — yeh woh globals/statics hain jo zero hain ya uninitialized (int y;). BSS ka mast point yeh hai ki yeh disk file mein jagah nahi leta, kyunki saare zeros ko save karne ka koi matlab nahi; OS load time pe khud zero bhar deta hai.

Beech ke bade khaali area mein do cheezein ek dusre ki taraf badhti hain. Heap (jo malloc/calloc se milta hai) upar ki taraf (higher address) grow karta hai, aur Stack (function calls, local variables, return address) neeche ki taraf (lower address) grow karta hai. Donon ek hi gap share karte hain, isliye koi bhi zyada use kar sakta hai — par agar dono takra jayein to overflow ho jata hai.

Sabse common confusion: global uninitialized variable C mein zero hota hai (BSS guarantee deta hai), lekin local uninitialized variable garbage hota hai (stack auto-zero nahi karta). Aur dhyan rakho: jab tum function ke andar malloc karte ho, to pointer stack pe hota hai par actual block heap pe, jo function khatam hone ke baad bhi zinda rehta hai — isliye tumhe free karna padta hai.

Yeh samajhna kyun zaroori hai? Kyunki memory bugs (dangling pointer, stack overflow, memory leak) sab isi layout se aate hain. Jab tumhe pata hota hai konsa data kahan rehta hai aur kab tak zinda rehta hai, tab tum sahi se debug kar pate ho aur efficient code likh pate ho.

Go deeper — visual, from zero

Test yourself — C Programming

Connections