r/C_Programming • • Oct 13 '26

Article Arena Allocators in C

https://eliasebner.com/blog/guides/arena-allocators-in-c/

I recently started implementing arenas in my programming and I thought that some beginners could very much benefit from this info. I wrote an article about it. For those that are unaware, arenas allow to greatly simplify the burden of managing memory manually in C. I hope it helps, tell me what you think!

55 Upvotes

26 comments sorted by

View all comments

1

u/rubidus-api Oct 14 '26

Good thread. A few points raised here match what I ran into while writing an arena for my own C library, so here's how I handled them.

skeeto's overflow. Here is my allocation path, trimmed down to three steps: alignment, padding, and the bounds check. No pointer is formed until the request is known to fit.

        typedef struct { unsigned char *buf; size_t cap, offset; } Arena;
    
        // align: a power of two
        void *arena_alloc(Arena *a, size_t size, size_t align)
        {
            // 1. alignment: on the real address, since the buffer itself may be unaligned
            uintptr_t base = (uintptr_t)a->buf, cur, aligned;
            if (ckd_add(&cur, base, a->offset) ||
                ckd_add(&aligned, cur, align - 1))
                return NULL;
            aligned &= ~(uintptr_t)(align - 1);
    
            // 2. padding
            size_t padding = aligned - cur;
    
            // 3. bounds: integers only, no pointer past the end is ever formed
            size_t required;
            if (ckd_add(&required, a->offset, padding) ||
                ckd_add(&required, required, size))
                return NULL;                        // overflow
            if (required > a->cap)
                return NULL;                        // out of memory
    
            a->offset = required;
            return a->buf + (required - size);      // in range by now
        }

Two differences from the code in the article:

  • Alignment is calculated on the actual address rather than the offset, since the backing buffer itself might not be aligned.
  • Every addition is checked (C23 ckd_add), so a huge size reports failure instead of wrapping. skeeto's 100-byte example (offset 97, 8-byte alignment) comes back as out of memory.

Provenance. The integer is only used to work out the padding. The pointer the function returns is a->buf plus an offset, never an integer cast back to a pointer, so it keeps the buffer's provenance and doesn't depend on how a compiler treats integer-to-pointer conversion, which WG14's provenance model (PNVI-ae-udi) is still pinning down.

Effective type is the other half. If the backing memory comes from malloc, it has no declared type, so each object carved out of it takes its effective type from the first store, and that's fine. If the backing is a declared array, like a static unsigned char buf[N] on a target with no heap, the strict reading of C's effective-type rules (6.5p6-7 in C11) is that the storage keeps its declared type, and accessing an int stored there through an int lvalue is undefined. It's a common pattern, but the standard doesn't bless it. If you want to stay within the letter of the standard, back the arena with malloc'd memory, or build with -fno-strict-aliasing.

WSWhitehouse's grow-in-place idea. My realloc does exactly that: if the block was the most recent allocation, it just moves the offset, with no copy.

Arena first, then a pool. I started with just the arena, which worked great for data that dies together. Then I hit its weak spot: data that gets allocated and freed over and over. Since free is a no-op, the arena keeps growing until the next reset (GrtflJello's point), and it never reuses an address it already handed out (roughly Rabbitical's point). So I added a pool. For the few data types that churn the most, a simple pool on top of the arena should cover it: keep freed items on a stack and hand back the most recently freed one first. The arena still owns the lifetime, and the pool recycles inside it. One rule: the pool's stack lives in the arena too, so a reset takes the pool with it, and you set it up again afterwards.

teleprint-me's ASan caveat is real, and my arena has it too. It doesn't poison memory, so ASan won't catch a use-after-reset. The pool at least fills freed items with 0xDD in debug builds.

And I agree with LeeHide: none of this is free performance, so measure first. For me the main win is lifetime management, not speed.

I picked up this explicit-allocator style from Luca Sas's ACCU 2021 talk Modern C and What We Can Learn From It (the slides call these "temporary allocators").