5.1.14 · HinglishC Programming

Dynamic memory — malloc, calloc, realloc, free

2,218 words10 min readRead in English

5.1.14 · Coding › C Programming


WHAT: Chaar functions

Sab <stdlib.h> mein declare hote hain. Yeh bytes mein kaam karte hain aur tumhe ek void * dete hain (ek generic address jo tum apne type mein cast karte ho).


WHY har ek exist karta hai (80/20 distinctions)


HOW: Sahi call ko first principles se derive karna

Tum kabhi memorize nahi karte "malloc ek number leta hai." Tum argument derive karte ho:

Figure — Dynamic memory — malloc, calloc, realloc, free

Common mistakes (Steel-manned)


Active Recall

Recall

free actually kya free karta hai? Yeh block ko heap allocator ki free list mein reuse ke liye return karta hai — yeh bytes wipe nahi karta aur tumhara pointer variable change nahi karta.

Recall

malloc(n * sizeof(*p)) ko malloc(n * sizeof(int)) se kyun prefer karein? sizeof(*p) automatically p ka type track karta hai, toh agar type change ho toh line correct rehti hai — kam bug surface.

Recall Ek case batao jahan

calloc strictly better hai malloc se. Jab tumhe zero-initialized memory chahiye (counters/flags); zyaadaatar implementations mein yeh size ko count/size mein split karta hai aur multiplication overflow detect karta hai (haalaanki woh overflow check implementation-defined hai, standard-mandated nahi).


Recall Feynman: 12-saal ke bachche ko samjhao

Ek library socho. Ek normal array aise hai jaise aane se pehle fixed table book karna — tumhe guess karna padta hai kitne dost aayenge. Heap librarian jaisi hai: tum jaate ho aur kehte ho "mujhe 5 kursiyan chahiye" abhi (malloc). Agar kursi ganди thi toh tum khud saaf karo; agar tidy librarian (calloc) se maango toh woh saaf kursiyan deta hai. Baad mein aur kursiyan chahiye? realloc badi jagah dhundh ke tumhara saamaan le jaata hai. Jaate waqt, tumhe kursiyan wapas karni hain (free) varna koi aur use nahi kar sakta — yahi "leak" hai. Aur wapas karne ke baad, unpar baithna band karo (pointer mat use karo), varna kisi aur ki lap milegi!


Connections

  • Pointers in C — dynamic memory pointers return karta hai; tumhe void * aur casting samajhna hoga.
  • Arrays vs Pointersa[i] malloc'd blocks par kaam karta hai pointer arithmetic ki wajah se.
  • The Stack and the Heap — automatic vs dynamic variables kahan rehte hain aur lifetimes kyun differ karti hain.
  • Memory Leaks and Valgrind — missing frees pakadne ke liye tooling.
  • sizeof operator — byte counts compute karne ka aadhar.
  • Undefined Behavior in C — use-after-free, double-free, out-of-bounds.

malloc/calloc/realloc/free kaunsa header declare karta hai?
<stdlib.h>
malloc se returned memory ka contents kya hota hai?
Uninitialized garbage (zeroed nahi)
calloc se returned memory ka contents kya hota hai?
Sab bytes zero set hoti hain
calloc(count, size) ke do arguments ka kya matlab hai?
elements ki sankhya aur har ek ka size; yeh count*size bytes allocate karta hai
Kya C standard guarantee karta hai ki calloc multiplication overflow detect kare?
Nahi — overflow handling implementation-defined hai; zyaadaatar real libcs check karke NULL return karte hain, lekin yeh standard-mandated nahi hai
malloc failure par kya return karta hai?
NULL
realloc kya preserve karta hai?
Purane block ka contents (chhote size tak, purana ya naya jo bhi chhota ho)
realloc ke saath temporary pointer kyun use karein?
Failure par woh NULL return karta hai lekin purana block rakhta hai; directly assign karne par purana block leak ho jaata
Dangling pointer kya hota hai?
Ek pointer jo already freed memory ka address hold karta hai
free(p) ke baad immediately kya karna chahiye?
p = NULL; set karo use-after-free aur double-free se bachne ke liye
Memory leak kya hota hai?
Allocated heap memory jo kabhi free nahi ki gayi aur jiska pointer kho gaya, toh reuse nahi ho sakti
Buffer grow karte waqt capacity double kyun karte hain?
Geometric growth se amortized O(n) total insertion cost milti hai O(n^2) ki jagah
malloc kaunsa type ka pointer return karta hai?
void *, C mein kisi bhi object pointer type mein implicitly convertible
Kya free(NULL) safe hai?
Haan, yeh ek defined no-op hai
malloc(n * sizeof(*p)) n elements ke liye kitne bytes request karta hai?
n × sizeof(*p) bytes

Concept Map

motivates

managed by

reserve uninitialized

reserve zeroed

resize block

return block

bytes from

does multiply and

may detect

preserves

then set

check

Runtime size unknown

Heap memory

stdlib.h functions

malloc n bytes

calloc count size

realloc ptr n

free ptr

n times sizeof deref p

Zero-filled memory

Overflow check impl-defined

Old contents

ptr equals NULL

NULL on failure