5.1.16 · D1 · HinglishC Programming

FoundationsStack frames — how function calls work at the memory level

2,586 words12 min read↑ Read in English

5.1.16 · D1 · Coding › C Programming › Stack frames — how function calls work at the memory level


0. "Memory" hai kya cheez? (the strip)

Kisi bhi fancy word se pehle, aapko EK picture chahiye: computer memory ek bahut lambi numbered boxes ki row hoti hai. Har box ek byte (ek chhota number) hold karta hai, aur har box ka ek address hota hai — uska ghar ka number.

Figure — Stack frames — how function calls work at the memory level

Addresses ki zaroorat hi kyun hai? Kyunki kisi variable par — ya code ki kisi line par — wapas aane ke liye aapko naam pata hona chahiye kahan rahta hai woh. Woh ek zaroorat (location ko naam dena) is poore topic ka beej hai. Addresses aapko dobara depth mein milenge Pointers and addresses in C mein.


1. Register — ek box jo CPU apni jeb mein rakhta hai

Main memory (lambi strip) door hai aur thodi slow bhi. CPU apne andar kuch ultra-fast boxes rakhta hai jinhein registers kehte hain. Yeh sirf kuch hi hote hain, har ek ka ek naam hota hai.


2. Stack — ek strip jise aap sirf ek end se touch karte ho

Memory strip lo aur ek rule pe agree karo: hum slots sirf ek end par add ya remove karein. Woh disciplined strip hi stack hai.

Figure — Stack frames — how function calls work at the memory level

3. Stack pointer — top par marker

Ab "top of stack" ko ek naam do. ek register hai jo current top slot ka address hold karta hai.


4. Function, call, aur return address

Yahan woh problem hai jo agla symbol existence mein laati hai: jab CPU kisi function mein jump karta hai, toh woh yaad kaise rakhta hai ki wapas kis line par jaana hai?

Figure — Stack frames — how function calls work at the memory level

5. Stack frame — ek function ki private desk

Ek call ko jo kuch bhi chahiye woh saath bundlo, aur aapko uska frame milta hai.


6. Base pointer — desk mein ek keel

chalti rehti hai (function ke dauran har push ise shift karta hai). Toh "meri teesri local" ko "SP minus kuch" ke roop mein address karna ek moving target hai. Ek doosra marker fix karo jo poori call ke liye wahan rahe: base/frame pointer .

Figure — Stack frames — how function calls work at the memory level

7. Prologue & epilogue — fixed opening aur closing moves


Symbols ka order (prerequisite map)

Yeh map bottom-up padho. Sabse baayi roots section 0 ki raw ideas hain (memory boxes aur addresses). Woh stack ko feed karte hain (ek disciplined strip), jise do register markers chahiye — (top) aur (fixed base). Woh markers, plus return address, exactly woh hain jo ek stack frame bundle karta hai, aur prologue/epilogue rituals woh hain jo har function us frame par perform karta hai. Koi bhi arrow follow karo: iska matlab hai "baad wali cheez samajhne ke liye pehle wali zaroorat hai."

memory strip of numbered boxes

address = a box number

slot = w bytes taped together

stack = one-end-only strip

register = fast box in CPU

LIFO from nested calls

SP marks top of stack

return address

stack frame per call

BP nails frame base

prologue and epilogue

Stack Frames topic


  • Frames stack par rehti hain; zyada time rehne wala data kahin aur rehta hai — Heap vs Stack memory dekho.
  • Stack par ek return address overwrite karo aur aap control hijack kar sakte ho — Buffer overflow & return-address smashing.
  • Deep nesting Recursion hai; raw addresses Pointers and addresses in C hain.

Equipment checklist

Right side cover karo aur zor se jawab do.

A byte is...
memory mein ek storage box jo ek chhota number hold karta hai.
An address is...
ghar-number (ek integer) jo ek memory box identify karta hai; bada = aage ki taraf.
A slot is...
w adjacent bytes/boxes ka ek fixed run jise ek unit maana jaata hai (jaise int ke liye 4 bytes).
A register is...
CPU ke andar ek fast named storage box (jaise SP, BP, LR).
The stack is...
memory ka ek region jisme sirf ek end par add ya remove hota hai.
"push" does what to SP (downward stack)?
SP ko ek slot size w se decrease karta hai, phir wahan value likhta hai.
"pop" does what to SP?
SP par padhta hai, phir SP ko w se increase karta hai — logically top slot discard karta hai.
LIFO stands for and comes from...
Last In, First Out; yeh function lifetimes ke perfectly nest hone se aata hai.
The stack pointer SP holds...
stack ke current top ka address.
The base pointer BP holds...
current frame ke andar ek fixed reference address, constant-offset access ke liye.
A return address is...
us instruction ka address jo called function khatam hone ke baad run hogi.
What does x86 CALL do?
function mein jump karta hai aur return address stack par push karta hai.
What does x86 RET do?
saved return address pop karke usi par wapas jump karta hai.
What do ARM BL / RISC-V JAL do?
function mein jump karte hain aur return address ek register (LR) mein rakhte hain, stack par nahi.
A stack frame contains...
ek call ki locals, arguments, saved registers, aur return address.
addr(local_k) = ... and why?
BP − offset; locals BP ke neeche baithe hain kyunki stack lower addresses ki taraf badhti hai.
addr(arg_k) = ... and why?
BP + offset; args BP se pehle push hue the, isliye woh higher addresses par hain.
Why save the caller's BP on entry?
BP ek single shared register hai; save/restore karne se caller apna frame rakhe rehta hai.
Prologue does...
old BP save karo, BP = SP set karo, local space banao (SP −= N).
Epilogue does...
SP = BP, old BP restore karo, RET.