# Arena Allocators in C: The End of Memory Leaks?

> Hacker News story 50046022 · language: en · submitted 2026-10-11
> Web version: https://zeli.app/story/50046022
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 86 · by [dimonomid](<https://news.ycombinator.com/user?id=dimonomid>) · 💬 38
[original](<https://eliasebner.com/blog/guides/arena-allocators-in-c/>) · [HN discussion](<https://news.ycombinator.com/item?id=50046022>)

## Summary

Arena allocators group memory with similar lifetimes into a single large chunk, then free it all at once. This guide walks through a complete C implementation, covering allocation, reallocation, and clearing, and shows how passing an arena pointer makes allocation explicit and lifetimes caller-controlled.

## Key quote

> This saves individual memory allocations (which remember, are slow), and makes us have to worry less about managing memory since we only have to worry about a couple of different “chunks” of memory instead of small pieces used for individual arrays.

## HN discussion (38 comments)

### [duckqlz](<https://news.ycombinator.com/item?id=50085214>)

> “Types.c” is dangerous code to bring to every project.
>
> f32/64 makes assumptions about the platform.
>
> Worse yet, the macros at the bottom are all functionally incorrect. Macros like MIN/MAX need to be contained in parentheses to avoid precedence errors.
>
> For example, we take `Min(x,y)` from the code. Consider what happens if I write
>
> `ans = 2 * Min(3,4)` because of the way Min is defined that will expand to `ans = 2 * 3 < 4 ? 3 : 4` and while you may expect ans to be 3 it will infact resolve to 4.
>
> Subtle bugs like these can creep into code and cause really hard to debug errors.
>
> Finally, while u32 is a little faster to type than `uint33_t` it makes the project harder to read forcing the reader to learn the author’s personal c dialect. While it is a stylistic choice, I can’t see why anyone would want to sacrifice code highlighting and expressiveness for slightly less verbosity

### [teo_zero](<https://news.ycombinator.com/item?id=50060347>)

> Please be aware that pointer arithmetic is not defined on void * pointers, although many compilers allow it. You had better use char * for "data".
>
> Also, your code is prone to overflow when you add data, used, alignment and size.
>
> And finally, the realloc function could be reworked to do nothing if new_size is <= old_size.

### [unwind](<https://news.ycombinator.com/item?id=50083931>)

> More obvious problems with the code as shown:
>
> \- The macros doing arithmetic (KiB, MIN etc) don't put their arguments in parentheses, that is very dangerous.
>
> \- The code assumes that int and i32 are the same size.
>
> \- Don't cast the return value of malloc(). Please Google this.
>
> I appreciated the use of structs by value, that's a nice design.
>
> I agree there's a risk of overflow in the arithmetic around sizes/alignment, but with a 64-bit `usize` that should be mostly theoretical.

### [sweetjuly](<https://news.ycombinator.com/item?id=50083599>)

> \> This will be used to align the data. For some reason, our computers are much better at dealing with data that is aligned at 8 bytes. This means that the start of each new allocation has to be at a multiple of 8 bytes.
>
> Casting a pointer which is not aligned to its natural alignment has undefined behavior in C. It's not just an issue of unaligned accesses being slow; in practice, your program might also crash or access unexpected data on real platforms with real compiler implementations.

### [jll29](<https://news.ycombinator.com/item?id=50088383>)

> See Sections 5+6 in https://drh.github.io/cii/toc.html

---

## Cite

- Page: https://zeli.app/story/50046022
- Date: 2026-10-11
- Source: eliasebner.com (https://eliasebner.com/blog/guides/arena-allocators-in-c/)
- Hacker News: https://news.ycombinator.com/item?id=50046022

## Other languages

- zh: https://zeli.app/zh/story/50046022 · markdown: https://zeli.app/zh/story/50046022.md
- ja: https://zeli.app/ja/story/50046022 · markdown: https://zeli.app/ja/story/50046022.md
- ko: https://zeli.app/ko/story/50046022 · markdown: https://zeli.app/ko/story/50046022.md
- de: https://zeli.app/de/story/50046022 · markdown: https://zeli.app/de/story/50046022.md
- es: https://zeli.app/es/story/50046022 · markdown: https://zeli.app/es/story/50046022.md
- ru: https://zeli.app/ru/story/50046022 · markdown: https://zeli.app/ru/story/50046022.md
