Foundations — Common memory errors — null dereference, buffer overflow, use-after-free, double free, memory leak
5.1.18 · D1· Coding › C Programming › Common memory errors — null dereference, buffer overflow, us
Kisi bhi memory error ko samajhne se pehle, tumhe parent note ke har symbol ko bina jhijhke padhna aana chahiye. Yeh page har ek symbol ko bilkul zero se build karta hai, ek aisi order mein jahan har idea apne pehle wale idea pe tika hota hai. Agar tumne kabhi C ki ek line bhi nahi likhi, yahan se shuru karo aur kuch bhi skip mat karo.
0. Woh picture jis par sab kuch tika hai: memory as a row of boxes

Figure dekho. Boxes ek horizontal strip mein draw kiye gaye hain. Har box ke neeche wala chhota number (0, 1, 2, 3, …) uska address hai — yeh kabhi nahi badlta. Box ke andar wali value data hai, aur woh kabhi bhi badal sakti hai jab tum usme likhte ho.
Topic ko yeh kyun chahiye: har single error — null deref, overflow, use-after-free — is baare mein hai ki tumne kaunsa box number touch kiya. Agar tum numbered boxes ki row nahi dekh sakte, toh koi bhi error samajh nahi aayega. Yeh picture ground floor hai.
1. byte symbol aur 0–255 ka range
Picture: upar wali strip mein ek box. 0–255 kyun? Ek byte 8 on/off switches (bits) hote hain; combinations, 0 se count karo, toh 0 se 255 milte hain. Yahan bit-level detail ki zaroorat nahi — bas itna jaano ki "ek box = ek byte = ek number ≤ 255".
Topic ko yeh kyun chahiye: jab parent note char buf[8] kehta hai to matlab hai 8 boxes ek saath. Jab woh kehta hai ek string ko "12 bytes" chahiye, to matlab 12 boxes. Buffer sizes bytes mein count hoti hain.
2. Ek variable — ek named box
Picture: strip mein ek box lo, uspe ek name tag x latkao. Tum x = 7; kaho aur number 7 us box ke andar chala jata hai.
3. int, char — kitne boxes aur kaisa shape
Picture: ek int char paas-paas ke boxes hain jo ek saath fence kiye gaye hain aur ek single value ki tarah treat kiye jaate hain.
Topic ko yeh kyun chahiye: sizeof(int) parent mein har jagah aata hai — yeh literally hai "ek int ko kitne boxes chahiye?" (jawab usually 4 hai). Aur malloc(sizeof(int) * 1000) ka matlab hai "mujhe 1000 ints ke liye enough boxes do".
4. & — "the address of" (box number)
Picture: x naam ke box par point karo, phir uske neeche print hua chhota number padho. Woh number &x hai.
Topic ko yeh kyun chahiye: yeh hai kahan ek value rehti hai. Kisi cheez par pointer point karana hai to uska address & se copy karo.
5. Pointer aur declaration mein *
Yeh poore topic ka star hai. Ise dheere dheere build karo.

Figure mein: x ek ordinary box hai jo address 100 par 7 hold karta hai. p ek aur box hai, aur p ke andar stored value number 100 hai — jo x tak pahunchne wale red arrow ki tarah draw ki gayi hai. Woh arrow hi pointer ka poora idea hai.
6. *p — dereference (arrow follow karo)
Picture: p par khade ho, andar wala number (100) padho, box #100 par chalo, use kholo. *p ab woh box hai. *p = 42; 42 ko x mein likhta hai. printf("%d", *p); x ki value read karta hai.
Topic ko yeh kyun chahiye: "null dereference" = box #0 ki taraf point karta arrow follow karna. "use-after-free" = ek aisi box ki taraf arrow follow karna jo tum pehle hi wapas kar chuke. Dereference shabd paanch error names mein se teen mein hai.
7. NULL aur address 0
Picture: ek pointer jiska arrow kisi box tak pahunchne ki bajaye crossed-out "🚫" ki tarah draw kiya gaya hai — woh reserved, forbidden box #0 ki taraf point karta hai.
Topic ko yeh kyun chahiye: operating system box #0 (aur uske aas paas) ko unmapped — untouchable — chhod deta hai, exactly isliye taaki ek NULL arrow follow karne par immediately crash ho (segfault), data silently corrupt hone ki bajaye. Woh controlled crash hi parent ka poora trick hai "free ke baad p = NULL; set karo taaki baad ka *p detectable ho".
8. Stack vs Heap — boxes ki do neighbourhoods

Figure ek strip ko do mein split karta hai: stack side (auto-managed) aur heap side (red block woh chunk hai jo tumne rent kiya). Stack par buffer overflows ek saved return address ko smash kar sakte hain (ek security disaster); leaks aur double-frees heap par hote hain.
Topic ko yeh kyun chahiye: paanch errors alag-alag neighbourhoods mein rehte hain. Yeh jaanna ki ek box kaun se region mein hai batata hai ki cleanup automatic hai (stack) ya tumhara kaam hai (heap). Dekho Stack vs Heap Memory.
9. malloc aur free — heap boxes rent karna aur wapas karna
Picture: malloc tumhe heap par ek red-outlined block aur ek kagaz ka tukda (p) deta hai jo uske pehle box ki taraf point karta hai. free(p) block un-reserve kar deta hai — lekin tumhare kagaz par abhi bhi number hai (ab ek dangling arrow). Dekho malloc and free.
10. Arrays, [], aur string terminator \0
Picture: 8 boxes ek saath fenced hain; offset i par box par ek red marker. Box 7 ke baad kadi aur tumne apne padosi ke boxes kuchalne shuru kar diye → overflow.
Topic ko yeh kyun chahiye: "hello world" 11 letters hai, isliye ise boxes chahiye. char buf[8] (sirf 8 boxes) mein yeh 4 se overflow hota hai — exactly parent ka example.
11. Undefined Behaviour — "mere machine par kaam kiya" prove kuch nahi karta
Picture: ek box jo tumhara nahi, touch kiya — teen arrows nikle hain jin par "crash", "corrupt", "theek lagta hai" likha hai. Tum choose nahi kar sakte kaunsa hoga. Dekho Undefined Behaviour.
Topic ko yeh kyun chahiye: isliye memory errors itne nasty hain. Yeh polite exceptions nahi hain — aaj ek use-after-free sahi answer print kar sakta hai aur kal ek file delete kar sakta hai.
Yeh foundations topic ko kaise feed karte hain
Us map mein har arrow ek prerequisite hai: tum paanch errors nahi samajh sakte jab tak L ke upar ka har node solid na ho. Related build-up notes: Pointers in C, malloc and free, Stack vs Heap Memory, Undefined Behaviour, aur baad mein Valgrind and AddressSanitizer aur Buffer Overflow Exploits.
Equipment checklist
Right side cover karo aur zabanī jawab do; check karne ke liye reveal karo.
Address kya hota hai?
Har variable box ki do cheezein kya hain?
&x tumhe kya deta hai?
x ka address (box number).Plain words mein, pointer kya hai?
*p ek expression mein kya karta hai?
p hold karta hai, aur woh box read/write karta hai.NULL ki numeric value kya hai, aur use touch karne par crash kyun hota hai?
0; OS box 0 ko unmapped chhod deta hai, isliye use dereference karne par immediately trap ho jaata hai.Stack boxes vs heap boxes kaunsa saaf karta hai?
free karna padta hai.malloc(n) kya return karta hai, aur failure par kya?
n nayi heap boxes mein se pehle ka address; agar allocate na ho sake toh NULL.Kya free(p) p mein stored value change karta hai?
p purana (ab dangling) address rakhta hai; sirf block wapas kiya jaata hai.char buf[8] ke liye aakhri legal index kya hai?
7 (indices 0 se 7 tak chalte hain — size se ek kam).L letters ki string ko L + 1 boxes kyun chahiye?
\0 (value 0) hold karta hai jo string ka end mark karta hai.