: the stack empties itself and the heap stays full until someone gives pieces back. This lesson builds the part that hands out the heap, and then measures how it goes wrong.
Before you start. The heap is the work area where a program keeps anything that must outlive one function call (How Code Becomes Execution (compilers, stack & heap)). The kernel gives memory only in whole pages, so the program must cut them into small pieces (The address book every process carries). A first allocator, with first fit, splitting, and merging free neighbours using a size tag at both ends of each piece, was built in Build malloc, then a garbage collector. The file system in A file system of your own tracked free space with a bitmap of equal blocks. Here the pieces have different sizes, so a bitmap is not enough.
Lesson 109 of 117 in From Scratch
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.