Master the fundamental concepts of c programming deep dive through this focused micro-challenge.
When you call malloc(100) on Linux, glibc's ptmalloc searches free lists, may mmap fresh pages from the kernel, and returns a pointer aligned to 16 bytes. Your exercise strips that down to a fixed char heap[4096] and an explicit Block header: size, used flag, and next pointer. That header-before-payload layout is the same idea jemalloc and tcmalloc use, just with fancier bookkeeping.
cLoading…
Returning user memory means skipping the header: (char*)block + sizeof(Block). Finding the header from a user pointer reverses that subtraction.
For this exercise, you will finish my_malloc and implement my_free with coalescing on a static buffer. This task asks you to see allocation as list surgery, because heap exploitation CVEs and allocator tuning both start with understanding these block headers.
Keep the relevant man page, ABI doc, or Rust reference chapter open while you work. When your output disagrees with the reference implementation on the same machine, the mismatch is usually an alignment rule, an off-by-one terminator, or a register slot you misread in GDB.
Implement my_malloc and my_free on top of a 4096-byte static array, the way a tiny embedded allocator works:
Then run the fixed scenario selected on stdin and print the heap.
struct Block { size_t size; int used; struct Block *next; }, which is 24 bytes on x86-64. size counts the payload bytes after the header.4096 - 24 = 4072.One word: alloc, free, merge, edge or multi.
After allocations: and the block list.After freeing p2: and the block list.Blocks merged and free_blocks=N, the number of free blocks left.my_malloc(0), my_malloc(4096) and my_free(NULL). Print zero=nonnull if the 0-byte request returned a pointer (zero=null_or_ok if it returned NULL), big=null if the 4096-byte request returned NULL (big=nonnull otherwise), then All edge cases pass. Under the rules above, a 0-byte request is rounded up to 8 bytes and succeeds, and 4096 bytes is more than the 4072 free bytes and fails, so the test expects zero=nonnull and big=null.All allocations successful and ok=1 if all eight succeeded (else ok=0).The block list is one line per block, in list order: Block I: used=U size=S.
Input:
cLoading…
Output:
cLoading…
malloc.Hidden tests cover freeing a middle block, coalescing into a single free block, zero-size and oversized requests, and many small allocations.
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