5.1.14 · D3 · HinglishC Programming

Worked examplesDynamic memory — malloc, calloc, realloc, free

4,170 words19 min read↑ Read in English

5.1.14 · D3 · Coding › C Programming › Dynamic memory — malloc, calloc, realloc, free

Yeh page parent topic ke liye drill-ground hai. Hum har tarah ke case se guzarte hain jo ek heap-allocation problem mein aa sakta hai: normal sizes, number zero, degenerate (empty) requests, allocation failure, growing, shrinking, byte count ka integer overflow, ek word problem, aur ek exam trap. Kuch bhi chhupaaya nahi gaya.

Aapko pehle se Pointers in C, sizeof operator, aur The Stack and the Heap se milna chahiye tha. Agar koi symbol unfamiliar lagta hai, toh woh cue hai unhe dobara dekhne ka.


Scenario matrix

Har heap call ko ek grid par ek point samjho. Do axes jo matter karte hain woh hain "main kitna maang raha hoon?" (aapka request kiya hua size) aur "allocator ka jawaab kya hai?" (ek real block, ya NULL). Yeh poora grid hai — har cell ka ek worked example neeche diya gaya hai.

Cell Situation Isme kya tricky hai Covered by
C1 Normal malloc of n objects, success byte count nikalna, garbage init Example 1
C2 calloc for zeroed data zero kyun matter karta hai, two-arg form, iska apna multiply overflow Example 2
C3 malloc(0)zero-size request NULL ya freeable non-NULL return kar sakta hai Example 3
C4 Allocation failure (NULL returned) + byte-count overflow check karna zaroori; product wrap ho sakta hai Example 4
C5 realloc se grow, block moves baad mein old pointer invalid, copy hoti hai Example 5
C6 realloc se shrink new size ke baad ka data lost ho jaata hai Example 6
C7 realloc(NULL, n), realloc(p, 0), realloc(NULL, 0)degenerate edges malloc/free/malloc(0) ki tarah behave karte hain Example 7
C8 Word problem: unknown-length input padhna grow-loop + free combine karo Example 8
C9 Exam twist: bug dhundho self-assign leak + overflow Example 9

Geometry ka sign/quadrant idea yahan sizes par map hota hai: positive-normal (C1, C2), zero (C3, C7), aur "answer is nothing" case (C4). Growing vs shrinking (C5, C6) change ki do directions hain. Ek hidden fourth axis yeh hai ki product n * sizeof(*p) khud malloc ke dekhne se pehle overflow ho jaata hai (C4, C9): ek "small-looking" request bhi wrap ho ke ek tiny number ban sakti hai aur aapko ek dangerously undersized block de sakti hai.

Neeche di gayi picture us poore grid ka map hai, toh ise dhyan se padho:

  • Horizontal line size axis hai — jaise aap right move karte ho waise aap kitne bytes maang rahe ho yeh badhta jaata hai. Left end size 0 hai, middle ek normal request hai, right end "too big to satisfy" hai.
  • Teen coloured dots teen answer-classes hain. Pink dot (far left) zero-size request hai (C3, C7) — allocator NULL ya freeable non-NULL answer de sakta hai. Blue dot (middle) normal success hai (C1 malloc, C2 calloc). Yellow dot (right) itni badi request hai ki allocator give up karta hai aur NULL return karta hai (C4).
  • Do arrows above the line realloc hain: blue arrow right point karta hai (grow, C5) aur pink arrow left point karta hai (shrink, C6)realloc literally is size axis par movement hai.
  • Axis ke neeche ek small fold-under lane hai jo hidden fourth axis hai: yeh dikhata hai ki ek bada request n × size wrap around ho ke zero ke paas aa jaata hai. Isliye jo request "yellow dot par land honi chahiye" woh secretly pink dot ke paas land kar sakti hai ek far too small block ke saath — yahi khatra C4 aur C9 warn karte hain.
Figure — Dynamic memory — malloc, calloc, realloc, free

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

Active Recall

Recall (C3)

malloc(0) ke do legal outcomes kya hain? Ya NULL, ya ek unique non-NULL pointer jise aap dereference nahi kar sakte lekin free kar sakte ho. Hamesha result free karo.

Recall (C2/C4)

SIZE_MAX kya hai, aur aap count * size ko overflow se kaise guard karte ho? SIZE_MAX sabse badi value hai jo ek size_t hold kar sakta hai. if (count > SIZE_MAX / size) ... se guard karo multiply karne se pehle — wrap hone ke baad detect nahi ho sakta. calloc ka apna overflow check sirf implementation-defined hai, guaranteed nahi.

Recall (C4)

malloc fail hone ke baad pointer ki value kya hoti hai, aur sizes size_t mein kyun compute karo? Failure ke baad pointer exactly NULL (integer 0) hai — hamesha pehle check karo. size_t use karo (long nahi) taaki n * sizeof(*p) usi width mein compute ho jo malloc expect karta hai aur 32-bit long platforms par silently wrap na ho.

Recall (C5 vs C6)

realloc ke baad kitne bytes of old data guaranteed intact hain? min(old_size, new_size) bytes. Growing sab kuch rakhta hai; shrinking sirf leading kept region rakhta hai.

Recall (C7)

realloc(NULL, n) aur realloc(NULL, 0) kya karte hain? realloc(NULL, n) malloc(n) ki tarah behave karta hai — ek fresh allocation. realloc(NULL, 0) malloc(0) mein reduce ho jaata hai — NULL ya ek freeable non-NULL pointer return kar sakta hai.

Recall (C8) Capacity 2 se start karke double karte hue, 5 items fit karne ke liye kitne doublings?

Do: 2 → 4 → 8.