5.1.15 · D1 · HinglishC Programming

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

2,130 words10 min read↑ Read in English

5.1.15 · D1 · Coding › C Programming › Memory layout — text, data, BSS, heap, stack segments

Aap "yeh variable BSS mein hai" kehne se pehle, aapko kuch choti-choti ideas mein fluent hona chahiye: byte kya hota hai, address kya hota hai, "read-only" ka matlab kya hai, "grows up" ka matlab kya hai. Parent note inhe silently assume karta hai. Yeh page inhe bilkul zero se, ek-doosre par depend karne ke order mein build karta hai.


1. Ek byte — memory ka atom

Yahan se kyun shuru karein? Kyunki topic mein sab kuch — ek variable, ek instruction, ek pointer — ultimately bytes ki ek run hai. Agar aap box ka picture nahi bana sakte, toh "4 MB in BSS" bekar ki baat lagegi.

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

Picture memory ko boxes ki ek vertical ruler ke roop mein dikhati hai. Har box ek byte hai. Har box ke baayein uska position number hota hai — woh number hi poora game hai, aur uska ek naam hai: address.


2. Ek address — box ka ghar ka number


3. Hexadecimal — kyun addresses 0x7ffe jaisa dikhta hai


4. Ek variable — kuch boxes par chipka hua naam


5. Initialized vs uninitialized — Data/BSS split, zero se

Yeh distinction hi wajah hai ki parent ke paas globals ke liye do alag segments hain. Ise carefully build karo.

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

Picture split ko obvious banati hai: 5 rakhne wale box ko disk par file mein physically woh 5 carry karna padega (→ Data segment). Ek box jo sirf "zero" hai use ek sticky note se describe kiya ja sakta hai — "N boxes reserve karo, sab zero" — disk par almost kuch nahi lagta (→ BSS segment). Yahi poori wajah hai kyun Static and global variables — storage duration Data ko BSS se alag karta hai.


6. Read-only vs writable — permission stamp


7. Growth direction — ruler par "up" aur "down"

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

Figure mein arrows dekho. Heap (burnt-orange) beech mein shuru hota hai aur uski top edge upar ki taraf chadhti hai har baar jab aap malloc karte hain. Stack (teal) ceiling ke paas shuru hota hai aur uski bottom edge neeche ki taraf sinkhti hai har baar jab ek function call hota hai. Woh dono unke beech wale empty gap ki taraf bhaagte hain — yahi parent ka "they share the gap in the middle" hai. Agar woh kabhi touch kar lein, aapko stack-overflow / heap-overflow milta hai, jo Buffer overflow and stack smashing se connect hota hai.


8. Do special markers — program break aur stack pointer


9. Static storage duration — "static" word ke do matlab hain


Prerequisite map

byte = one box

address = box number

hexadecimal 0x notation

variable = name on boxes

initialized vs zero

Data vs BSS split

read-only permission

Text segment shared

grows up vs grows down

program break and stack pointer

storage duration

static vs automatic

Memory layout topic

Ise top-down padho: byte se address milta hai, address se position aur direction ke har notion milte hain, aur permission + duration decide karte hain ki kaun se segment mein ek value land karti hai. Charon streams Memory Layout topic mein pour hote hain.


Equipment checklist

Recall Self-test: kya aap jhankne se pehle har ek ka jawab de sakte hain?

Byte kya hota hai, ek sentence mein? ::: Memory ka sabse chota named chunk, jo 0 se 255 tak ek number hold karta hai. "Address" ka matlab kya hai? ::: Memory ruler par ek byte ka position number. &x aapko kya deta hai? ::: Woh address (box number) jahan x shuru hota hai, uski value nahi. 0x2000 "higher" kyun hai 0x1000 se? ::: Yeh ek bada number hai, isliye yeh ruler par aage ek box hai. Ek typical int kitne bytes ka hota hai? ::: 4. Ek initialized global disk space kyun leta hai lekin zero wala nahi? ::: Non-zero value file mein store ki gayi real information hai; all-zeros ko sirf size note se describe kiya jata hai. "Read-only" aapko kya karne deta hai aur kya forbid karta hai? ::: Bytes read karne deta hai, lekin kabhi write nahi (likhna crash karta hai). "Heap grows up" addresses ke baare mein kya matlab hai? ::: Har naya heap block higher address par land karta hai. "Stack grows down" addresses ke baare mein kya matlab hai? ::: Har naya call frame lower address par land karta hai. Heap ki top edge kaun sa marker track karta hai? ::: Program break. Stack ki top edge kaun sa marker track karta hai? ::: Stack pointer (e.g. rsp). Kya sab globals ka static storage duration hota hai? ::: Haan, static keyword ke bina bhi.


Connections

  • यह Hinglish में पढ़ो →
  • Static and global variables — storage duration — Data vs BSS rule jis par yeh foundations lead karte hain.
  • malloc, calloc, realloc and free — kaise program break heap ko move karta hai.
  • Stack frames and the calling convention — stack pointer kya push karta hai.
  • Pointers and dangling pointers — addresses jab box apne naam se zyada jeeta hai.
  • Virtual memory and paging — yeh ruler real hardware se kaise map hota hai.
  • Buffer overflow and stack smashing — kya hota hai jab growth collide karta hai.