Scope, lifetime aur storage duration
Naam kahan visible hai aur object kab exist karta hai, alag samjhein; phir safe pointer use se jodein.
Is chapter mein10 sections
Naam scope se bahar ho sakta hai jabki uska object abhi exist karta ho. Object ki lifetime end hone ke baad bhi pointer mein unusable purani value ho sakti hai. Scope aur lifetime in situations ko alag samjhate hain.
Ideas ko saath rakhkar samajhna hai? Concept guide kholein.
1. Teen questions, teen concepts
| Question | Concept | Kya describe karta hai |
|---|---|---|
| Yeh naam kahan use kar sakte hain? | Scope | Program text ka region. |
| Object kab exist karta hai? | Lifetime | Execution ka time period. |
| Kya declarations same entity refer karti hain? | Linkage | Declarations ke beech relationship. |
Scope identifiers ka concept hai, physical memory region nahi. Lifetime objects ka concept hai, naam abhi visible hai ya nahi iska nahi. Linkage teesra question answer karta hai; woh memory amount ya function call ki duration nahi. Same keyword, jaise static, different contexts mein different cheezon ko affect kar sakta hai.
2. Naam hide hone se object erase nahi hota
Yahan assert, <assert.h> ka check hai. Assertions enabled hon toh false condition failure report karke abort karti hai; invalid operation safe nahi banta. Yeh fragment function ke andar ka context dikhata hai.
int score = 4;
{
int score = 9;
assert(score == 9);
}
Inner declaration doosra object introduce karti hai aur inner scope mein outer identifier ko hide karti hai. Woh outer object overwrite nahi karti. Inner block ke baad outer naam phir visible hai aur value 4 hai. Isse shadowing kehte hain. Beginner ke liye distinct names usually clearer hote hain, lekin example batata hai naam aur object ko alag kyun rakhna chahiye.
3. Storage duration lifetime rules ko group karti hai
Storage duration ki chaar categories hain: automatic, static, thread aur allocated. Non-VLA automatic object ki lifetime generally associated block mein entry se us block ki execution end hone tak hoti hai. Initialization aur name visibility ke source text mein apne positions hote hain. Variable-length array ki lifetime ka start alag hai: uski declaration. Har recursive entry ke apne automatic instances hote hain.
Static-storage objects poori program execution mein exist karte hain. Thread-storage objects ki lifetimes corresponding thread se tied hoti hain; later concurrency material access rules samjhayega. Allocated objects ki lifetimes allocation aur deallocation se control hoti hain. In categories ke liye stack, data segment ya heap naam ke particular hardware regions required nahi.
4. Local visibility ke saath long lifetime ho sakti hai
#include <assert.h>
static int next_count(void)
{
static int count = 0;
count += 1;
return count;
}
int main(void)
{
assert(next_count() == 1);
assert(next_count() == 2);
return 0;
}
count identifier sirf apne block mein visible hai, lekin object do calls ke beech value retain karta hai. Har entry par initializer dobara apply nahi hota. Block-scope static object ke identifier ka no linkage hota hai; file-scope static function name, jaise next_count, ka internal linkage hota hai. Yeh separate declarations mein static ke separate effects hain.
5. Pointer target ki lifetime extend nahi karta
Yeh pointer notation ka chhota preview hai: &total, total ko point karta hai aur *view usko access karta hai. Next lesson notation detail mein samjhayega; yahan dekhein kaunsa object alive rehta hai.
int total = 4;
{
int *view = &total;
*view = 7;
}
Yahan inner pointer use hone ke time outer object alive hai. Pointer ka apna block pehle end hota hai, jo valid hai. Relationship reverse karna dangerous hoga: ordinary local object ka address longer-lived jagah store karna local object ko alive nahi rakhta. Uska address return karne se lasting object create nahi hota.
Object ki lifetime end hone par usko ya uske just-past position ko point karne wale pointers C17 rule ke under indeterminate ho jaate hain. Stale pointer value ko dereference, print, compare ya otherwise rely na karein. Still-live pointer object ko old value read kiye bina fresh valid value ya null pointer assign kar sakte hain. Isse sirf woh pointer reset hota hai; doosre aliases repair nahi hote.
Primary references aur aage padhne ke liye
- WG14 N1570 — public C11 committee draft: 6.2.1, 6.2.2, 6.2.4. Yeh stable rules C17 baseline ko support karte hain; yeh draft final ISO publication nahi hai.
Optional visual model: Visibility aur lifetime alag samjhein
Visibility aur lifetime alag samjhein
VISUAL MODEL
Inner block ke ordinary local aur static local ko follow karein. Names aur objects alag track karein.
Inner block ek chal rahe function ke andar hai. local non-VLA automatic int hai. kept ki static storage duration hai aur initialization program startup se pehle hoti hai.
/* inside a function, before the inner block */{static int kept = 2;int local = 3;/* both names are visible here */}/* outside the inner block */
kept2Alive; naam scope se baharlocal—Lifetime shuru nahi; naam hiddenInner block se pehle kept alive hai, par uska naam yahan scope mein nahi. local ki lifetime shuru nahi hui.
Scope batata hai naam kahan visible hai. Lifetime batati hai object kab exist karta hai. Ordinary non-VLA automatic object ki lifetime block entry par shuru hoti hai, initializer tak pahunchne se pehle.
Poora trace padhein
- Inner block se pehle kept alive hai, par uska naam yahan scope mein nahi. local ki lifetime shuru nahi hui.
- Block mein enter karne se local ki lifetime shuru hoti hai. Yahan uska naam abhi declare nahi hua aur initializer nahi chala.
- kept ki declaration uska naam scope mein laati hai. Usmein pehle se 2 hai; block entry use dobara initialize nahi karti.
- local ki declaration naam visible karti hai aur initializer 3 store karta hai. Dono objects alive hain.
- Yahan dono names visible aur dono objects alive hain. Yeh alag properties hain.
- Block chhodne par local ki lifetime end hoti hai. kept alive rehta hai, par yahan dono names apne scope se bahar hain.
- kept ab bhi value 2 ke saath exist karta hai. Model uska state batata hai; yahan code out-of-scope naam directly use nahi kar sakta.
Naam kahan visible hai aur object kab exist karta hai
Idea compare karein, follow karein aur samjhayein
Scope batata hai source code mein identifier kahan use ho sakta hai. Lifetime execution ke dauran object ke exist karne ka time batata hai. Storage duration us lifetime ko determine karne wale rules deta hai.
Yeh kab kaam aata hai Blocks, local static objects, recursion aur function se bahar jaane wale pointers padhte waqt yeh questions poochhein.
Ideas ko saath rakhkar padhein
- Scope
- Source code ka region jahan particular naam apni declared entity ko refer kar sakta hai.
- Lifetime
- Execution ka time jisme object exist karta hai aur uska storage reserved hai.
Naam hidden hone ka matlab zaroori nahi ki object dead hai.
- Automatic local fresh
- Yahan har active call ka naya object hota hai. Initializer tak pahunchne par 0 store hota hai.
- Static local total
- Ek object poore program tak rehta hai aur calls ke beech apni value rakhta hai.
Dono names ka block scope hai; objects ki storage duration alag hai.
- Naam aur initializer
- fresh ka scope source mein uske declarator ke turant baad shuru hota hai. Execution jab bhi declaration tak pahunchta hai, uska automatic initializer chalta hai.
- Block mein enter karna
- Ordinary non-VLA automatic object ki lifetime associated block mein entry par shuru hoti hai.
Name visibility, lifetime start aur initialization ko ek event na samjhein.
Reasoning ko follow karein
Alag statements mein visit ko do baar call karein. Return number mein total tens position aur fresh units position par hai.
C17 · diye gaye context ke illustrative fragments
int visit(void) {static int total;int fresh = 0;++total;++fresh;return total * 10 + fresh;}
- Calls se pehle
Static object total pehle se exist karta hai aur program startup se pehle zero-initialized hai. Uska naam sirf declared block mein visible hai.
- First call shuru karein
Is call ka apna automatic fresh object hai. Declaration tak pahunchne par initializer 0 store karta hai.
- Increment aur return
total 1 aur fresh 1 hota hai. Function 11 return karta hai; block chhodne par us call ke fresh ki lifetime khatam hoti hai.
- Second call shuru karein
Wahi total object abhi bhi 1 rakhta hai. Is call ka naya fresh object 0 se initialize hota hai.
- Dobara increment karein
total 2 aur naya fresh 1 hota hai. Second result 21 hai. Yahan dono automatic objects ko ek saath exist karna zaroori nahi.
Common misconceptions
Yeh statements misleading hain. Kyun, yeh samajhne ke liye har statement kholein.
MisconceptionHar local object declaration line execute hone par banta hai.
Ordinary non-VLA automatic objects ki lifetime block entry par shuru hoti hai; declaration baad mein naam introduce aur initialization kar sakti hai. VLAs ke lifetime rules alag hain.
MisconceptionLocal static object har call mein dobara banta hai.
Naam ka scope local hai, par object ki storage duration static hai. Is C example ki initialization C++ jaisa first-call operation nahi hai.
MisconceptionPointer return karne se local object ki lifetime badh jaati hai.
Pointer value return karne se target object ki lifetime extend nahi hoti. Ordinary local object ko uska function return hone ke baad access na karein.
Thoda aur depth meinOptional technical detail
Scope identifiers ke baare mein hai
Object ka apna naam out of scope ho, tab bhi valid pointer se us tak pahunch sakte hain. Us object ki lifetime ko alag se access permit karna chahiye.
Model ko machine ki promise na banayein
“Stack local” useful implementation vocabulary hai, C mein automatic storage duration ki definition nahi. C in objects ke liye ek physical stack layout mandate nahi karta.
C17 Walkthrough mein ordinary automatic int objects aur ek zero-initialized static int hai. VLAs, longjmp, threads aur compound-literal lifetime details is example mein nahi hain.
C23 C23 mein bhi scope aur lifetime ka difference apply hota hai. Naya syntax expired automatic object ke pointer ko valid nahi banata.
Yeh public committee drafts hain; clause references se explanation verify kar sakte hain. N3096 ek C23 working draft hai.
Apne words mein samjhayein
Second call mein fresh phir 0 kyun hota hai, jabki total 1 rakhta hai?
Ek explanation se compare karein
Second call ka naya automatic fresh object hai, jiska initializer 0 store karta hai. Static total object calls ke beech exist karta rehta hai.
Aage badhne se pehle
- Scope naam ki visibility aur lifetime object ke exist karne ka period hai.
- Storage duration lifetime rules deti hai; universal stack diagram nahi.
- Address rakhne se pointer target object ki lifetime extend nahi karta.
In ideas ko alag rakhein
- Name unavailable / Object no longer alive
- Naam scope se bahar ho sakta hai jabki static object rehta hai; target lifetime end hone se stale pointer use ka alag problem hai.
Ordinary automatic local object ka address return karne par caller ko usable target kyun nahi milta?
Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.Apni understanding check karein
Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.
Har choice ki explanation padhein
- Object alive rehta hai, par yahan woh naam apne block scope se bahar hai.
✓ Sahi reasoning. Static storage duration aur block scope alag sawaalon ke answers hain.
- Block exit par object destroy hona zaroori hai.
Yeh ordinary automatic storage se related hai, static local ki storage duration se nahi.
- Uska naam globally visible ho jaata hai.
Static storage duration block-scope identifier ko file-scope identifier nahi banati.
C17
Agla idea padhne ke liye tayyar?
Mark this chapter as read whenever you feel ready.