Almost every program asks the system for memory and hands it back when it's done. In February 2026 I wrote that system myself in C, working inside one fixed 4 KB block of memory: hand out space, take it back, reuse it, and catch programs that hand back memory incorrectly.
Why the simple design
The memory is split into chunks laid out one after another. To hand out space, the program walks through the chunks and takes the first free one that's big enough, splitting it if there's room left over. When space is handed back, it's merged with any free neighbors so the memory doesn't break into pieces too small to use.
Allocate a few blocks, then click one to free it.
0 of 4096 bytes in use. Each chunk carries a 16-byte header (illustrative size); payloads are 8-byte aligned.
There are faster designs, and I chose not to use them. With only 4 KB, walking through every chunk is cheap, so I traded speed for something small enough to understand completely and test thoroughly. For a real general-purpose system with lots of memory, that trade wouldn't hold.
Rejecting pointers that look valid
The trickiest part was catching mistakes. It's easy to reject a request to free memory that's nowhere near the block. It's harder to catch one that points into the middle of something already in use, which would quietly corrupt everything if accepted. So before freeing anything, the program checks that the request points exactly at the start of a block it handed out, and it reports anything freed twice.
I tested it with targeted cases and with random workloads run over and over, and the random runs were the most useful, because they produce situations I'd never think to write by hand. The bigger lesson was about bugs in general: a lot of crashes that seem random are a broken rule somewhere much earlier, and checking things at the boundary is how you catch them close to the cause.