Foundations — Valgrind — detecting memory errors
5.1.19 · D1· Coding › C Programming › Valgrind — detecting memory errors
Parent note Valgrind — detecting memory errors padhne se pehle, tumhe usmein use hone wale har word ko khud se samajhna padega. Neeche, har idea ekdum scratch se build kiya gaya hai: plain words → ek picture → kyun topic ko yeh chahiye. Upar se neeche padho; har rung uske neeche wale pe khadi hai.
1. Ek byte aur ek address — numbered boxes ki row
Ek lambi row of lockers ki picture socho. Har locker ek byte hai. Har locker door pe lika chhota number uska address hai. Jab C "memory access" karta hai, woh ek locker (address) pe jata hai, aur ya toh reads karta hai kya andar hai ya ek naya value writes karta hai usmein.

2. Ek pointer — ek box jo ek box-number store karta hai
Notation:
int *p— padho "pek pointer hai ekintki taraf."*declaration mein matlab hai "yeh cheez kisi cheez ki taraf point karti hai."*p— padho "us locker ke andar ki value jis parppoint karta hai." Yahan*ka matlab hai "wahan jao aur andar dekho." Ise dereferencing kehte hain.p(akela) — locker number khud.

Topic ko yeh kyun chahiye: parent note *p == 0 aur a[5] = 42 likhta hai. Dono pointer operations hain. a[5] secretly *(a + 5) hai — "jahan a point karta hai wahan se 5 slots aage jao, phir andar dekho." Zyada detail mein Pointers in C.
3. Stack vs Heap — memory ke do neighbourhoods
Do neighbourhoods ki picture socho:
- Stack — trays ka ek stack. Har function call ek tray upar rakhti hai; jab woh return hoti hai, uski tray automatically phek di jaati hai. Tum yahan kabhi
freenahi call karte. - Heap — ek warehouse jahan tum storage space maangote ho ("mujhe 20 bytes do") aur tum usse wapas dene ke zimmedaar ho.
Valgrind ka poora kaam heap ko dekhna hai, kyunki heap wahan hai jahan tumhare paas zimmedari hai — aur isliye jahan tum galtiyan karte ho. Dekho Stack vs Heap.
4. malloc aur free — heap boxes rent aur return karna
Do crucial facts jinpe parent note heavily depend karta hai:
- Ek fresh
mallocblock mein garbage hota hai — jo bhi junk pehle un lockers ko use karne wale ne chhoda tha. C use clear nahi karta. free(p)ke baad, pointerpabhi bhi purana address hold karta hai, lekin tum ab use use karne ke allowed nahi ho. Use touch karna = use-after-free.
Zyada detail: malloc and free.
5. sizeof aur array indexing — counts ko byte offsets mein badalna
sizeof(int) usually 4 hota hai (har integer ke liye char bytes). Toh parent ke overflow example mein, malloc(5 * sizeof(int)) bytes rent karta hai: lockers se tak (block ke start ke relative).
Phir a[5] ka matlab hai byte — block ke baad pehla locker. Exactly isliye Valgrind kehta hai "0 bytes after a block of size 20": tumhara write precisely wahan se start hota hai jahan rented block khatam hota hai.

Recall Quick check: ek
int array ke liye a[3] kaun sa byte likhta hai?
Bytes 12, 13, 14, 15 — yaani, se tak. 20-byte block ke andar, yeh safe hai.
6. Do shadow labels — A-bits aur V-bits
Ab payoff. Valgrind har real byte ke liye do hidden tags rakhta hai:
Do alag sawaal:
| Situation | A-bit | V-bit | Valgrind error |
|---|---|---|---|
| Ek freed box read karna | 0 (tumhara nahi) | — | Invalid read (use-after-free) |
Fresh malloc value pe branch karna |
1 (tumhara) | undefined | "Conditional jump depends on uninitialised value" |
| Array ke ek past likhna | 0 (block ke baad) | — | Invalid write |
Parent A-bit rule ko formally kehta hai: address pe n bytes ka access OK hai sirf tab jab har byte ka A-bit accessible set ho. Agar ek bhi 0 hai, toh error hai.
7. Undefined Behaviour — kyun "yeh kaam kar gaya" kuch prove nahi karta
Picture: UB ek coin hai jo heap layout ki kismat se zyada din heads (kaam karta hai) pe land karta hai. Compiler, input, ya kuch aur badlo, aur yeh tails (crash / corruption) pe land karta hai. Valgrind exactly UB ko pakadne ke liye exist karta hai isse pehle ki yeh kisi customer ke liye tails pe flip ho. Dekho Undefined Behaviour in C.
Prerequisite map
Baayein taraf har foundation daayein taraf ke shadow-memory idea ko feed karta hai. Valgrind sirf tab meaningful hai jab addresses, pointers, heap, aur UB sab apni jagah baith jaayein. Related tooling jo baad mein dekhne layak hai: AddressSanitizer (ASan) aur Debugging with GDB.
Equipment checklist
Ek address physically kya hai?
int *p kya declare karta hai vs *p kya karta hai?
int *p, p ko ek int ki taraf pointer declare karta hai; *p use dereference karta hai — us box pe jata hai jis par p point karta hai aur uski contents read/write karta hai.Valgrind mainly kaun sa memory region dekhta hai, aur kyun?
malloc/free se manually manage karte ho, isliye wahan tumhari galtiyan rehti hain.Ek fresh malloc block ke andar kya hota hai?
malloc use clear nahi karta.Ek typical machine par malloc(5 * sizeof(int)) kitne bytes rent karta hai?
sizeof(int) 4 hai aur 5 × 4 = 20.