Master the fundamental concepts of memory optimization through this focused micro-challenge.
General-purpose malloc carries metadata, coalescing, and thread-cache logic. Games, network stacks, and kernels use memory pools when objects are one size and lifetimes are short. PostgreSQL buffer pools and slab allocators in Linux follow the same pattern at different scales.
Preallocate a slab of N objects. Free list threads through objects with an embedded next pointer. Allocate and free are pointer swaps.
cLoading…
Keep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will implement a fixed-size memory pool with allocate and free in O(1). This exercise asks you to beat malloc on a microbenchmark creating many same-sized objects.
A fixed-size memory pool hands out equal-sized slots in O(1). Free slots form a singly linked list, and the "next" pointer is stored inside the free slot itself, so the pool needs no extra memory for bookkeeping. Implement the pool, optionally growing by whole chunks, and detect double frees.
cLoading…
0x10000 * k, and slot s is at chunk base + (s mod CAPACITY) * slot_size. Slots are numbered globally: chunk k holds slots k*CAPACITY, k*CAPACITY + 1, …, new chunk), otherwise print alloc NAME: pool exhausted (NULL). A name that is already live prints alloc NAME: name in use.free NAME: not allocated (double free or bad pointer).pool: size >= 1, capacity 1..256 is printed for bad parameters.cLoading…
pool adds , grows to its header when growth is on. free list: empty is printed when the list is empty. Peak is the largest number of slots in use at once.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover LIFO reuse after several frees, growth across three chunks, turning growth on after the pool is full, tiny objects rounded up to 8 bytes, invalid pool parameters, and freeing names that were never allocated.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works