5.1.17 · D3 · HinglishC Programming

Worked examplesHeap fragmentation

2,526 words11 min read↑ Read in English

5.1.17 · D3 · Coding › C Programming › Heap fragmentation

Yeh Heap fragmentation ka "sleeves roll up karo" wala child note hai. Parent ne tumhe ideas diye the — external vs internal fragmentation, coalescing, block-size formula. Yahan hum un saari ideas ko concrete numbers ke through drive karte hain, aur deliberately har corner case ko hit karte hain jo yeh topic throw kar sakta hai: block formula ke har sign, zero-size aur degenerate inputs, jab requests bahut badi ho jaati hain toh limiting behaviour, ek real-world word problem, aur ek exam-style trick.

Kisi bhi example se pehle, chalte hain un do tools ko apne words mein dobara earn karte hain jinpar hum lean karte hain.


Scenario matrix

Neeche har worked example matrix cell ke saath tagged hai jise woh cover karta hai, taaki tum dekh sako ki poora space fill hai.

Cell Kya vary karta hai Extreme jo probe karta hai Example
A External-fail free total ≥ request, holes non-adjacent classic NULL-despite-free Ex 1
B Coalesce-save adjacent order mein free holes merge hokar ek ho jaate hain Ex 2
C LIFO vs random same allocs, do free orders order fate decide karta hai Ex 3
D Internal ek boundary overshoot karta hai worst-case waste Ex 4
E Internal exactly boundary par land karta hai zero-waste degenerate Ex 5
F Zero / tiny input header-only block Ex 6
G Limiting behaviour large waste fraction Ex 7
H Word problem game-server churn real-world external frag Ex 8
I Exam twist "total free = request ⇒ success?" counter-layout se disprove karo Ex 9

Hum ke har sign ko touch karte hain (D mein positive, E mein zero), zero input (F), large limit (G), dono free orders (C), aur fragmentation ke do flavours (external: A,B,C,H,I; internal: D,E,F,G).


Ex 1 — Cell A: external fragmentation NULL force karta hai

Figure — Heap fragmentation

Ex 2 — Cell B: coalescing usi request ko bachata hai


Ex 3 — Cell C: same allocations, do destinies

Figure — Heap fragmentation

Ex 4 — Cell D: internal fragmentation, worst-ish case


Ex 5 — Cell E: zero-waste boundary case


Ex 6 — Cell F: zero-size request malloc(0)


Ex 7 — Cell G: limiting behaviour, huge request


Ex 8 — Cell H: real-world word problem

Figure — Heap fragmentation

Ex 9 — Cell I: exam twist (ek plausible claim ko disprove karo)

Recall Quick self-test

malloc(n) ki success kaunsi quantity par depend karti hai? ::: Largest single contiguous hole ki size, total free bytes par nahi. r=20, h=8, a=16 ke liye, wasted bytes kitne hain? ::: 12 r=24, h=8, a=16 ke liye, rounding waste kitna hai? ::: 0 (sirf 8-byte header overhead hai) malloc(0) with h=8, a=16 kitne size ka block carve karta hai? ::: 16 bytes (sab overhead) r badhne par W/B kya ho jaata hai? ::: 0


Connections

  • Heap fragmentation — parent topic jin examples ko yeh drill karta hai
  • malloc and free — first-fit allocator jo har example simulate karta hai
  • Stack vs Heap — kyun LIFO (Ex 3) kabhi fragment nahi hoti
  • Memory alignment — block formula mein (Ex 4–7)
  • Pool allocator / Arena allocator — Ex 8 ke fixes
  • Garbage collection — compaction jo Ex 8 ko rescue karta
  • Memory leaks — contrast: leaked ≠ fragmented