Align. Check. Reserve.
C11 fixed-arena exercise · about 15–20 minutes.
Align two slices in a 64-byte arena: a 3-byte request starts at 0, then a 5-byte request starts at 8. Implement the two functions and run the seven real C tests below.
Guest code stays in this browser tab, not in an account. Results and completion are unsaved. Copy your code before closing the tab; browser session restore may retain the draft.
Download the unfinished C starter, the same seven checks, and C11 compile/run instructions. No account or network connection is needed to run it locally.
Download starter + tests · ZIP ↓Local work is not automatically synced to an account. Return here for the guided explanation; paste your code into the browser editor and run tests in your account to save completion and continue.
Local compile/run instructions
Unzip, open a terminal in the heapcraft-arena-alignment folder, and use a C11 compiler such as GCC or Clang.
cc -std=c11 -Wall -Wextra -pedantic learner.c tests.c -o alignment-tests
./alignment-testsEdit learner.c, then compile and run again. The starter intentionally fails four checks. See README.md for Windows instructions and the return link to this lesson.
01 / Alignment and a byte arena
C11Build the first part of a fixed-arena allocator: choose an aligned starting offset and reserve a slice without crossing the arena’s end.
1. A byte is the unit
An unsigned char arena[64] owns 64 consecutive C bytes. arena + offset points to a byte inside it. The cursor used records where the next search begins; it is a byte offset, not a pointer.
2. Align the start, not the size
The test arena is declared _Alignas(8). Start each slice at a multiple of 8. After a three-byte request at offset 0, the cursor is 3; the next slice starts at 8. The five skipped bytes are padding.
3 bytes3–7
padding8–12
5 bytes13–63
remaining
For offsets 0 through 64, rounding up can be written ((used + 7) / 8) * 8. Integer division drops the remainder. Here used + 7 is safe because used ≤ 64.
3. Check before adding
Implement arena_take. Reject zero bytes. Find the aligned start. A request fits only if bytes ≤ 64 - start. Then set *used = start + bytes and return start. On any failure, return SIZE_MAX without changing *used.
size_t is unsigned. A huge start + bytes can wrap; check the remaining capacity instead. The tests supply a valid cursor pointer with *used in [0, 64].
Why not cast every slice to a struct?
Eight-byte alignment is this exercise’s policy, not a universal guarantee for every C type. We only store raw bytes. A general allocator needs alignment suitable for its supported types, object-lifetime/effective-type reasoning and more bookkeeping. This arena has no free or reuse yet.
learner.c
Guest · not saved to accountReal GCC 13.2 C11 tests run in Compiler Explorer’s separate sandbox. Run tests sends your code there, without account details. Do not include secrets. Free service availability varies; 30-second request limit, 8 seconds between runs, 20 runs per UTC day per shared network; shared capacity: 8 starts/30 seconds and 500/day. Guest code is not saved on Heapcraft.
Test results
Run your implementation to see compiler diagnostics and 7 checks. Tests are not run until you choose Run tests.