Build your own Memory Allocator
Your own malloc, free, calloc and realloc on top of sbrk or mmap, loaded into real programs with LD_PRELOAD.
- Intermediate
- About 6–12 h
- 4 checkpoints
- 3 tutorials · C
Pick a tutorial, then check yourself at each checkpoint. Work in any language you like.
Choose a tutorialWhy build it
Heap bugs such as use-after-free, double free and fragmentation make sense once you have managed block headers and free lists yourself.
You will be able to
- Explain how the heap grows with sbrk or mmap and what a block header stores.
- Implement a free list with block splitting and coalescing.
- Reason about alignment, fragmentation and the cost of an allocation strategy.
- Debug memory errors with Valgrind or AddressSanitizer.
Skills it exercises
Recommended on Operating Systems & Linux.
How you know you are there
Work in any language. Each checkpoint ends with something you can run or observe, so you can check it yourself.
- Checkpoint 1Self-check
Bump allocator
Done when: malloc returns 16-byte-aligned memory from sbrk, and a test allocates 10,000 blocks of random size and writes every byte without crashing.
- Checkpoint 2Self-check
Free and reuse
Done when: Freed blocks go on a free list and are reused first-fit; in an allocate/free loop the heap stops growing.
- Checkpoint 3Self-check
Split and coalesce
Done when: Large free blocks are split, neighbours merge on free, and a fragmentation test reports heap size before and after.
- Checkpoint 4Self-check
Real programs
Done when: With calloc and realloc added,
LD_PRELOAD=./libmymalloc.so ls -laand a Python one-liner run correctly on your allocator.
Stretch goals
- Size-class bins for small allocations.
- Thread safety with a lock, then per-thread arenas.
- mmap for large allocations.
Step-by-step guides
Pick one in a language you know (tutorial languages: C). They open on the author’s site.
- C
Malloc tutorial(external site)
Compact walkthrough: sbrk, a free list, and testing with LD_PRELOAD.
- C
Malloc is not magic -- Implementing your own memory allocator(external site)
A second explanation with diagrams of block headers.
- C
Hack the virtual memory(external site)
Background: reads /proc/<pid>/maps to show where the heap lives.
Practice builds are self-paced: Tech Atlas does not track them or count them as evidence. To show a skill, take its knowledge check from the skill page.
Tutorials come from codecrafters-io/build-your-own-x (CC0, snapshot of 2026-07-14, commit aa17439; links checked on 2026-10-19). Each tutorial belongs to its author and keeps its own licence; Tech Atlas links to it and does not host it. Briefs, checkpoints and the mapping to skills are Tech Atlas editorial drafts.