Malloc is NOT Magic: Let’s Build it to Learn What’s Inside!

Malloc is NOT Magic: Let’s Build it to Learn What’s Inside!

More

Descriptions:

Dave’s Garage builds a C memory allocator from scratch, starting from the simplest implementation imaginable and incrementally exposing why production allocators like glibc’s ptmalloc or jemalloc have decades of accumulated complexity. The goal is not just to explain what malloc does but to make every design decision feel inevitable by demonstrating what breaks without it.

The video opens with a bump allocator — a few lines of C that return the current pointer and increment it — and then systematically dismantles it: no bounds checking causes writes into unmapped memory, missing alignment logic corrupts SIMD and double-precision types, and the absence of any free mechanism means memory is never reclaimed. Each failure mode introduces the next layer. A free list with block headers enables reclamation, but immediately raises the fragmentation problem: enough total free memory may exist while no single contiguous block is large enough to satisfy an allocation request.

The two canonical solutions — splitting (carving only the needed portion from a larger free block) and coalescing (merging adjacent free blocks on release) — are implemented and explained, along with boundary tags that allow backwards traversal through the heap to find mergeable neighbors. The video concludes with size-class binning, the technique used by real allocators to avoid linear scans across thousands of blocks. Throughout, the video draws on analogies — parking garages with variably sized cars, neighbors redecorating the same kitchen simultaneously — to make memory safety hazards like use-after-free and double-free tangible. This is foundational knowledge for anyone working on inference runtimes, embedded systems, or systems programming at the infrastructure layer.


📺 Source: Dave’s Garage · Published May 16, 2026
🏷️ Format: Deep Dive

1 Item

Channels