Unit 02 of 03
Dynamic Memory
Allocation, zero-filled allocation and resizing functions, documenting the deallocation contract in a header file, measuring a memory leak by a counter difference, whether a tool catches it or not, and memory being divided into automatic, static and dynamic regions.
Published Updated
Lessons
- 01 Memory Allocation Functions malloc, calloc, realloc, and free all work from the same heap and all four leave the same question: who frees the block you allocated? The answer is always the same — the allocator is the caller; this lesson measures that with the counter difference, reads the zeroing directly, and shows that resizing leaves the old ownership unchanged. 14 min read
- 02 Deallocation Discipline When a function returns a piece of text, who owns that text is not written in the signature — what writes it is the comment in the header file. Two functions pointing at the same record, one lending it and one returning a copy, show how the same contract can bind to two different owners. 13 min read
- 03 Memory Leaks A memory leak gives no error message and crashes no program — it only widens the gap between what was allocated and what was freed. This lesson produces a leak two different ways, catches both with the counter difference, and shows why the total comes out to exactly 2. 6 min read
- 04 Memory Debugging Tools Finding a leak by holding a counter by hand is not possible in a real program — a tool that does this is run instead. This lesson shows that the counting wrapper the course has used from the start already models the one thing such a tool does: is there a free for every allocation, or not. 11 min read
- 05 Memory Layout A program's memory is not one single region; it is split into separate areas — automatic, static, and dynamic — and which region a block sits in decides from the start who its owner will be. This lesson lines up the four language observations side by side with the allocate-and-free observation and measures this distinction without printing an address. 11 min read