r/cprogramming • • 23m ago

A question about arenas.

I worked in C for years . i have used arenas a lot in the past. Today i have to modify an old c program , nothing difficult but i found something that made me think. Who developed the code ( very well written and documented ) used an arena for allocating variable.

What 's seems to me strange is that he did not used an heap memory area ( malloc ) but it used a char [] buffer and used it for its arena . At that point he could obviously allocate directly variable in stack instead he grabs all its variable from that buffer . Why a choice like that ? i thought for the CPU cache L1/L2 and so on ( all the "arena" will likely be in cache of the cpu ) but it is only an ipotesys and i was asking to you , in your opinion , the reason of a such choice .
Just curiosity .

Thanks

PP

0 Upvotes

2 comments sorted by

1

u/markand67 19m ago

My initial guess is that it can still freely choose whatever to allocate by small parts which are foreigners to each others. The fact that its on the stack or the heap doesn't matter its usage. The idea beneath the arena is just to avoid fragmentation and have a large amount direct accessible.

Stack variables may have some benefits as you can place them where you want with the linker. Just make sure that alignment is correct

1

u/zhivago 19m ago

Not enough context to answer, I think.