मुख्य सामग्री पर जाएँ

सिलेबस · CS कंप्यूटर विज्ञान और सूचना प्रौद्योगिकी

विषय के सभी टॉपिक
C प्रोग्रामिंग की बुनियाद7
ऐरे और पॉइंटर9
रिकर्शन6
प्रायिकता और सांख्यिकी की बुनियाद9
डेटाबेस सिस्टम की बुनियाद15
रैखिक बीजगणित की बुनियाद6
कंप्यूटर नेटवर्क की बुनियाद9
कलन की बुनियाद6
संगणना सिद्धांत की बुनियाद9
कंपाइलर डिज़ाइन की बुनियाद9
लिंक्ड लिस्ट, स्टैक और क्यू5
ट्री, बाइनरी सर्च ट्री और हीप4
ग्राफ की बुनियाद3
एल्गोरिदम की बुनियाद15
असतत गणित की बुनियाद18
डिजिटल लॉजिक की बुनियाद22
कंप्यूटर संगठन की बुनियाद12
ऑपरेटिंग सिस्टम की बुनियाद15

Runtime environments: activation records और static links

पाठ 175 / 1796 मिनटमुफ़्त

लक्ष्य और assumptions

Procedure calls, recursion, lexical nesting और storage lifetimes समझें। यहाँ conventional stack implementation, हर active call का अलग frame, lexical/static scope तथा explicit static और dynamic links मानेंगे। Fields का क्रम और byte offsets implementation पर निर्भर हैं। Size वाले प्रश्न में alignment और widths दिए होने चाहिए। हर real compiler यही layout रखता है, ऐसा दावा नहीं।

1. Storage duration और scope

Whole-program lifetime वाले objects का static storage होता है। Stack allocation nested call lifetimes सँभालता है: call लौटने पर उसका frame हटता है। Heap अधिक सामान्य lifetimes देता है, जैसे function के return के बाद भी इस्तेमाल होने वाला object। Scope बताता है name किस declaration को दर्शाता है; lifetime बताती है object कब मौजूद है। ये केवल source lines का range नहीं हैं।

Recursion में एक procedure की कई activations साथ जीवित होती हैं। f(3) से f(2), फिर f(1) call हों तो body एक है लेकिन f की तीन activations हैं। Parameters और nonstatic locals के अलग storage चाहिए। Recursive ordinary local x को एक ही static slot देने से दूसरे active calls की x values overwrite होंगी।

2. Frame fields और call trace

Frame में parameters, return-value location, return address, saved registers, locals, temporaries, dynamic link और static link हो सकते हैं। Dynamic/control link caller के activation record को दिखाता है और उसका environment restore करने में मदद देता है। Static/access link callee के lexical enclosing procedure की active frame को दिखाता है, ताकि static scope के nonlocal variables मिलें।

Factorial-जैसे f(n) में f(3)→f(2)→f(1) होता है, f(1) से 1 लौटता है। Stack पहले f(3),f(2),f(1) तक बढ़ता है, फिर f(2), 21=2 तथा f(3), 32=6 करके लौटते हैं। Return address बताता है caller कहाँ resume करे। वह static link या integer return value नहीं है।

Size उदाहरण: fixed fields 24 bytes, प्रति parameter 8 bytes, locals 16 bytes, extra alignment padding नहीं। एक parameter का frame 24+8+16=48 bytes है। पाँच साथ active calls 240 bytes लेंगे; outer frame इसमें नहीं है। Peak stack के लिए समयभर हुए कुल calls से multiply न करें।

3. Static और dynamic link का worked उदाहरण

Lexical nesting में Main के अंदर P और R हैं; P के अंदर Q है। Main में x, P में y है; Q दोनों उपयोग करता है। Runtime में Main→P→Q call होता है। Q का dynamic link P पर है क्योंकि P caller है। Static link भी P पर है क्योंकि P lexical parent है। y पाने के लिए Q एक static link और x के लिए Q→P→Main दो static links चलता है।

अब Q, R को call करे, जो Main में declared है। R का dynamic link Q को दिखाएगा, लेकिन static link Main को। R में Main.x एक static link से मिलेगा, dynamic chain R→Q→P→Main से नहीं। Caller और lexical parent अलग हो सकते हैं।

Q खुद Q को recursively call करे तो नए Q का dynamic link पुराने Q को दिखाएगा; static link enclosing P activation को, पुराने Q को नहीं। Static links declaration nesting दर्शाते हैं, recursion depth नहीं। Callee का static link देने के लिए caller उसके lexical parent की current activation खोजता है, जरूरत पड़े तो अपनी static chain पर चलता है।

4. Escaping environment और parameters

Function value में code address तथा environment reference का pair हो सकता है, जिसे closure कहते हैं। Nested function बाहर लौटकर enclosing call खत्म होने के बाद भी उसके variables पढ़े तो सामान्य stack reclamation पर्याप्त नहीं: environment बचना चाहिए, जैसे heap में। Dead stack local का address वापस कर देने से उसका lifetime नहीं बढ़ता।

Call-by-value में argument value की copy मिलती है। Value pointer हो तो pointed-to object बदलने से caller-visible object बदल सकता है; local pointer parameter को फिर assign करने से caller का pointer variable नहीं बदलता। Call-by-reference में parameter caller की storage location को दर्शाता है। Variable बदलना और pointer से object बदलना अलग रखें।

सावधानियाँ

Static scope का अर्थ static allocation नहीं। Stack किस दिशा में बढ़ता है, machine पर निर्भर है। Display lexical levels की indexed alternative structure है, dynamic link का दूसरा नाम नहीं। Optimization frames हटा सकती है या locals registers में रख सकती है; प्रश्न के abstract model के अनुसार गणना करें, platform ABI न गढ़ें।

मौलिक अभ्यास: छह single-correct MCQ

ये मौलिक शिक्षण प्रश्न हैं, पिछले वर्षों के प्रश्न नहीं। ठीक एक उत्तर चुनें.

P01. Main में P,R और P में Q है। Q, R को call करे तो R का static link कहाँ होगा?

A. Q B. P C. Main D. स्वयं R

उत्तर: C

A: Q caller है, dynamic link target।

B: P, R का lexical parent नहीं।

C: R सीधे Main में declared है।

D: Static link enclosing activation को दिखाता है।

P02. P में Q अपने-आप को call करे तो नए Q का dynamic link किस पर है?

A. पिछली Q activation B. हमेशा Main C. हमेशा P D. किसी frame पर नहीं

उत्तर: A

A: पिछला Q caller है।

B: Main ने यह call नहीं किया।

C: इस उदाहरण में P static-link target है।

D: Caller activation मौजूद है।

P03. 24 fixed, 8 parameter और 16 local bytes वाली पाँच active frames कितना space लेंगी?

A. 48 bytes B. 120 bytes C. 200 bytes D. 240 bytes

उत्तर: D

A: यह एक frame है।

B: इसमें केवल fixed fields हैं।

C: पूरी frame 48 है, 40 नहीं।

D: No-padding में 5×(24+8+16)=240।

P04. Scope और lifetime का सही अंतर क्या है?

A. Scope हमेशा stack duration है B. Scope declaration चुनता है; lifetime object existence बताती है C. Static scope में सभी locals static होने चाहिए D. Address लौटाने से हर local lifetime बढ़ती है

उत्तर: B

A: दोनों अलग हैं।

B: Name binding और storage existence अलग प्रश्न हैं।

C: Lexical binding के साथ stack allocation संभव है।

D: Dangling address local को जीवित नहीं रखता।

P05. Enclosing call लौटने के बाद उपयोग होने वाली nested closure को क्या चाहिए?

A. केवल पुराना return address B. जरूरी values जीवित रखने वाला environment C. बिना सुरक्षा reclaimed stack bytes का reuse D. Captured variables पढ़े तब भी environment नहीं

उत्तर: B

A: Return address captured values नहीं बचाता।

B: Environment को original frame से अधिक जीना होगा।

C: वे bytes overwrite हो सकते हैं।

D: Captured variables के लिए environment चाहिए।

P06. Pointer parameter call-by-value से मिले तो क्या caller-visible object बदल सकता है?

A. केवल local pointer फिर assign करें B. Pointer से shared object में लिखें C. केवल local integer copy बदलें D. बिना write लौटें

उत्तर: B

A: इसमें केवल parameter copy बदलती है।

B: Copy address वही object दिखा सकता है।

C: Local copy अलग है।

D: यहाँ mutation नहीं।

स्रोत और सीमा

Official 2026 course के अनुसार विषय का सीमित भाग। 2027 की पूर्णता का दावा नहीं। सभी उदाहरण तथा अभ्यास मौलिक हैं.

प्रगति सहेजने के लिए साइन इन करें। साइन इन