5.2.13 · D1 · HinglishC++ Programming

FoundationsRAII — resource acquisition is initialization — why it's the key idiom

1,787 words8 min read↑ Read in English

5.2.13 · D1 · Coding › C++ Programming › RAII — resource acquisition is initialization — why it's the

Yeh page har woh word build karta hai jo parent note RAII topic use karta hai, bilkul zero se. Agar tumne kabhi C++ class nahi likhi, toh yahan se shuru karo.


0. "Resource" kya hota hai?

Ek library book ki tarah socho. Tum use check out karte ho (acquire) aur return karna zaroori hai (release). Agar tum kabhi return nahi karte, koi aur use borrow nahi kar sakta — book "leaked" ho gayi.

Figure — RAII — resource acquisition is initialization — why it's the key idiom

Resources ke examples, sab usi borrow-return shape mein:

Resource "Check out" "Return"
Heap memory new delete
File fopen fclose
Lock lock() unlock()

Yeh topic isko kyun chahiye: RAII poori tarah "return" step ko automatic banane ke baare mein hai. Agar acquire→release rhythm tumhare andar nahi basi, toh aage kuch bhi samajh nahi aayega.


1. Stack, scope, aur { }

{ aur } decoration nahi hain — yeh birth aur death ke markers hain.

{
    int x = 5;   // x yahan paida hota hai
    // ... x usable ...
}                // x MARTI hai yahan — uska slot reclaim ho jaata hai
// x ab exist nahi karta
Figure — RAII — resource acquisition is initialization — why it's the key idiom

"Stack" kyun? Plates ke stack ki tarah: tum sirf upar se add ya remove kar sakte ho. Jo plate sabse baad rakhi gayi woh pehle uthaayi jaati hai. Yeh last-in-first-out order exactly yahi reason hai ki baad wale objects pehle wale objects se pehle destroy hote hain (parent ka Example 3).

Yeh topic isko kyun chahiye: RAII "ek resource ko stack par rakhe object se baandhta hai." Yeh sentence meaningless hai jab tak tum nahi jaante ki stack object closing brace par automatically marta hai — wahi automatic death woh hook hai jis par RAII apna cleanup laatakata hai.


2. Objects, aur do special functions

~ (tilde) matlab hai "constructor ka ulta" — undo function.

class Book {
public:
    Book()  { /* checkout: acquire */ }   // constructor — birth par run hota hai
    ~Book() { /* return:  release  */ }   // destructor — death par run hota hai
};

Yeh topic isko kyun chahiye: yeh do functions HI RAII hain. Poori mechanics ke liye Constructors and Destructors dekho.


3. Pointers, new, delete, aur nullptr

Figure — RAII — resource acquisition is initialization — why it's the key idiom
  • new T system se fresh heap memory maangta hai aur uski taraf ek pointer return karta hai (acquire).
  • delete p woh memory wapas karta hai (release).
  • nullptr woh special "kuch bhi point nahi karta" address hai — ek pointer jo jaanboojhkar void ki taraf aimed hai.

Yeh topic isko kyun chahiye: heap memory classic resource hai. new/delete classic manual acquire/release hai jise RAII unique_ptr se replace karta hai — Smart Pointers - unique_ptr shared_ptr dekho.


4. Exceptions aur stack unwinding

Yeh "har line ko skip karna" exactly wohi hai jo manual delete p; lines ko kill karta hai.

Figure — RAII — resource acquisition is initialization — why it's the key idiom

Figure dekho: jab bhi red "throw" bolt mid-function fire karta hai, already-built objects ke pale-yellow destructors phir bhi bahar jaate waqt run hote hain. Yeh guarantee RAII ka engine hai. Ek raw delete p; jo plain statement ke roop mein pada hai woh destructor nahi hai, toh unwinding use run nahi karti — lekin ek unique_ptr ka destructor hai, toh woh karta hai.

Yeh topic isko kyun chahiye: stack unwinding woh single language promise hai jo RAII ko exception-safe banata hai. Aur gehraai mein: Stack Unwinding and Exceptions aur Exception Safety Guarantees.


5. Copy, move, aur "ownership"

Do cheezein accidentally ek doosra owner bana sakti hain:

"Cheen lo phir null karo" kyun? Move ke baad, source harmless hona chahiye. Uska pointer nullptr par set karne ka matlab hai uska destructor "kuch bhi point nahi karta" dekh ke delete skip kar deta hai — toh resource ek baar free hota hai, sirf naye owner ke zariye.

Yeh topic isko kyun chahiye: yahi reasoning parent note mein Rule of Five ke peeche hai. Rule of Three Five Zero aur Move Semantics dekho.


Prerequisite map

Resource: acquire then release

RAII idiom

Scope and the stack

Objects with ctor and dtor

Pointers, new, delete, nullptr

Ownership: exactly one owner

Exceptions

Stack unwinding

Copy vs Move: Rule of Five

Har foundation RAII ko feed karta hai: scope+objects hook dete hain, unwinding guarantee deti hai, ownership correctness rule deta hai.


Equipment checklist

Ek phrase mein "resource" kya hota hai?
Kuch bhi jo acquire karna aur baad mein release karna padta hai (memory, file, lock, socket).
Stack variable ka birth aur death kya mark karta hai?
Opening { (birth) aur closing } (death) uske scope ka.
Stack objects kis order mein marte hain?
Birth order ka reverse — last constructed, first destroyed (LIFO).
Kaunsa function object birth par automatically run hota hai? Death par?
Birth par constructor; death par destructor (~ClassName).
new kya return karta hai, aur delete kaun call kare?
Fresh heap memory ki taraf pointer; delete tumhe khud call karni padti hai.
nullptr kya hai?
Ek pointer jo jaanboojhkar kuch bhi point nahi karta.
Exception kya skip karta hai?
Current function mein throw ke neeche ki har line jab woh jump out karta hai.
Stack unwinding kya hai?
Scope chodne par, C++ living objects ke destructors ko reverse order mein run karta hai control exit hone se pehle.
Golden ownership rule kya hai?
Exactly ek owner resource ko exactly ek baar free karta hai.
Kisi owner ko copy karna double free kyun cause kar sakta hai?
Dono objects ek hi pointer hold karte hain, toh dono destructors us par delete call karte hain.
Move source ke pointer ke saath kya karta hai?
Use nullptr par set karta hai taaki source ka destructor kuch na kare.