r/cprogramming • u/Elifire12 • Oct 13 '26
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!
27
Upvotes
4
u/flatfinger Oct 13 '26
Applications that reuse storage as different types should always use -fno-strict-aliasing to avoid an "ABA" optimization bugs when where a storage is used as one type, then as a second, and then again as the first. When not using that flag, neither compiler reliably handles the sequence:
If clang or gcc happens to notice that 1 and 5 are accessing the same storage, and that 3 and 4 are accessing the same storage, but doesn't happen to notice other relationships, it may consolidate the read in step 5 with the access in step 1.
While it's not particularly likely that the compiler would happen to notice and not notice a problematic combination of things, use of the -fno-strict-aliasing flag would make the code build correctly by specification rather than happenstance.
1
u/orange_oki Oct 17 '26
Haven't done C for years, so it can be buggy, but maybe you like it... It's pretty much like PostgreSQL does it.
https://github.com/ykat-UG-haftungsbeschrankt/c-malloc-context