Mistakes and cool things to do with arena allocators
Published on: 2025-04-27 19:08:50
When programming in Odin you can use arena allocators. If you use an arena allocator combined with a dynamic array, then there are a couple of pitfalls that may not be apparent at first. Let’s look at what arenas are, how you can run into trouble when naively using them with dynamic arrays and what you can do instead.
What’s an arena? How does it work?#
Arenas and arena allocators are useful for grouping allocations that have the same lifetime. Here lifetime means allocations that are fine to deallocate at the same time, which can be done by destroying the arena they were allocated into.
The arena is the thing that holds the memory and the arena allocator says how to allocate into the arena. The allocator is of type runtime.Allocator , so that you can pass it around to things that expect an allocator.
There are several different implementations of arenas in Odin. Here’s a list of some of them:
In core:mem : mem.Arena , mem.Dynamic_Arena
: , In core:mem/virtual : virtual.Arena (ha
... Read full article.