Make memory management visible, one byte at a time
This group project implements a 127-byte heap simulator in C. The small fixed heap makes allocation behavior inspectable: every header, payload, footer, split, and merge has a concrete address.
We represented the heap as an unsigned char array and used an implicit list of blocks. Each block has a one-byte header and footer. Seven bits encode its total size, including that metadata; the remaining bit indicates whether it is allocated.
Allocation is a tradeoff
The simulator offers First Fit by default and Best Fit as a launch option. First Fit takes the first free block that can satisfy a request. Best Fit searches for the smallest suitable block.
A suitable free block can be split, but only when the remainder can form another valid block. A remainder of fewer than three bytes cannot hold the two metadata bytes and a payload byte, so the allocator keeps the entire block together.
Freeing needs its neighbors
Freeing a block clears its payload and examines adjacent blocks. Free neighbors are coalesced, and the merged region receives a new matching header and footer. The footer makes the previous block’s size available when walking backward.
The interactive commands include malloc, free, and blocklist, alongside raw memory read and write operations. The listing orders blocks by payload size and address, making fragmentation easier to observe.
Example
> malloc 10
1
> malloc 5
13
> free 1
> blocklist
106-20-free
10-1-free
5-13-allocated
The returned pointers identify the first payload byte. The listed sizes are payload sizes; the sizes encoded in the headers and footers include the metadata.
Build and scope
gcc -std=c11 -O2 -Wall -Wextra -o alloc hw4.c
./alloc
./alloc BestFit
The educational simulator follows the assignment’s permissive raw read/write model. Those commands expose the heap directly and can overwrite metadata, making block invariants an important part of inspecting its behavior.
What I learned
A tiny heap made it easier to see the cost of bookkeeping and the effects of fragmentation. It also made invariants tangible: after a split or merge, the block boundaries and both metadata bytes must agree. The project helped connect an abstract allocation strategy with the low-level representation that makes it work.