5.1.15 · HinglishC Programming

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

1,980 words9 min readRead in English

5.1.15 · Coding › C Programming


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 Peeking se pehle try karo
  • Kaun sa segment read-only hota hai? Text.
  • int x; global kahaan rehta hai? BSS.
  • int x = 9; global kahaan rehta hai? Data.
  • Kya BSS disk space leta hai? Nahi (sirf ek size record hota hai).
  • Heap kis direction mein barhta hai? Upar (higher addresses).
  • Stack kis direction mein barhta hai? Neeche (lower addresses).
  • malloc ka block kahaan rehta hai? Heap.
  • Ek function ka local int kahaan rehta hai? Stack.
Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho tumhare program ko ek bada bookshelf milta hai. Sabse neeche wali shelf mein instruction manual hota hai — tum padh sakte ho lekin uspar kuch likh nahi sakte (Text). Agle shelves mein pehle se bhari hui toys hoti hain (Data) aur "zeros se bharo" label wale khaali boxes (BSS) — khaali boxes ko shifting truck mein pack karne ki zaroorat nahi, tum bas ek sticky note par "10 khaali boxes" likh dete ho. Beech ke bade space mein, toy pile neeche se upar barhta hai jab bhi tum naya toy maangte ho (Heap = malloc), aur plates ka stack chhat se neeche barhta hai jab bhi tum naya kaam shuru karte ho aur ek plate rakhte ho (Stack = function calls). Jab kaam khatam ho, plate automatically wapas chali jaati hai. Agar toy pile aur plate stack kabhi aapas mein takra jaayein — room khatam!


Connections

  • malloc, calloc, realloc and free — heap kaise manage hota hai.
  • Stack frames and the calling convention — stack push ke andar kya hota hai.
  • Static and global variables — storage duration — Data vs BSS rules.
  • Pointers and dangling pointers — heap lifetime bugs.
  • Virtual memory and paging — segments physical RAM se kaise map hote hain.
  • Buffer overflow and stack smashing — stack ki security side.
Which segment stores compiled machine instructions and is read-only?
Text (code) segment.
Where do initialized non-zero global/static variables live?
Data segment mein.
Where do uninitialized or zero-initialized globals/statics live?
BSS segment mein.
Why does BSS take no space in the executable file?
Yeh sirf ek reserve karne wala size store karta hai; OS ise load time par zero-fill karta hai, isliye all-zero contents disk par save nahi karne padte.
What is the default value of an uninitialized GLOBAL variable in C?
Zero (BSS mein jaata hai, zero-initialize hona guaranteed hai).
What is the value of an uninitialized LOCAL (auto) variable?
Indeterminate/garbage — stack par hota hai aur auto-zeroed nahi hota.
Which direction does the heap grow?
Higher addresses ki taraf (program break upar jaata hai brk/sbrk se).
Which direction does the stack grow?
Lower addresses ki taraf (stack pointer push par decrease hota hai).
When you call malloc inside a function, where is the returned block, and where is the pointer?
Block heap par hota hai; pointer variable stack par hota hai.
What tracks the top of the heap?
Program break, brk/sbrk se adjust hota hai.
What tracks the top of the stack?
Stack pointer register (jaise rsp).
What causes a stack overflow vs heap exhaustion?
Stack overflow = bahut gehri recursion/bade locals (SP limit se aage); heap exhaustion = malloc NULL return karta hai.

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