Foundations — RAII — resource acquisition is initialization — why it's the key idiom
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.

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
"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

new Tsystem se fresh heap memory maangta hai aur uski taraf ek pointer return karta hai (acquire).delete pwoh memory wapas karta hai (release).nullptrwoh 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 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
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?
Stack variable ka birth aur death kya mark karta hai?
{ (birth) aur closing } (death) uske scope ka.Stack objects kis order mein marte hain?
Kaunsa function object birth par automatically run hota hai? Death par?
~ClassName).new kya return karta hai, aur delete kaun call kare?
delete tumhe khud call karni padti hai.nullptr kya hai?
Exception kya skip karta hai?
throw ke neeche ki har line jab woh jump out karta hai.Stack unwinding kya hai?
Golden ownership rule kya hai?
Kisi owner ko copy karna double free kyun cause kar sakta hai?
delete call karte hain.Move source ke pointer ke saath kya karta hai?
nullptr par set karta hai taaki source ka destructor kuch na kare.