Last chapter left you with a small worry. You made an array of 100 student records, and it worked, but only because you already knew the number 100. Now picture the admission desk of a coaching centre in Mirpur. For the Friday mock test, twelve students turn up. For Saturday's, a hundred and forty. A program with room for exactly 100 records wastes 88 of them on Friday and turns away 40 students on Saturday. The number you need isn't known when you write the program. It becomes known only when the program runs.
C's answer is to let the program ask the system for memory while it is running: exactly as much as it needs, no more, and to hand it back when it is done. This is dynamic memory allocation. It comes with a catch that nothing else in this subject has: nobody cleans up after you. If you forget to hand memory back, the program slowly eats the machine. If you hand it back twice, or keep using it after handing it back, the program may crash today or quietly give wrong answers next month. So this chapter teaches the tool and the discipline together.
By the end you'll be able to say where a variable lives and why that matters, and to use malloc, calloc, realloc and free correctly, including growing an array while a program reads its input. You'll build one-dimensional arrays, two-dimensional tables, strings and arrays of records on the heap. You'll recognise the classic heap bugs, watch each one happen in a real run, and write the exam answer to "describe dynamic memory allocation". The next lesson uses these blocks as the links of a chain and builds linked lists from them.
We'll go in four parts: where data lives (the stack and the heap); malloc and free; calloc, realloc and what you can build with them; and what goes wrong and how to catch it. Every program here was compiled and run, and every output you see is the real one.