r/C_Programming • u/swe__wannabe • Jul 16 '26
Etc We have automatic cleanup at home
So you have a simple bump (arena) allocator:
typedef struct {
u8* base;
u64 idx;
u64 size;
} Arena;Now you define a Scratch struct, something that holds the arena state at a certain point where we can revert back to:
typedef struct {
Arena* arena;
u64 mark;
} ArenaScratch;Then you can do something cool:
#define ARENA_SCRATCH(arena_ptr) \
for (ArenaScratch __nme__ = arena_scratch_begin(arena_ptr); \
(__nme__ ).arena != NULL; \
arena_scratch_end((__nme__ )), (__nme__).arena = NULL)Where begin/end functions simply save the state/revert back to it.
for loop inside a macro without any body enables scoped cleanup.
Usage:
ARENA_SCRATCH(arena) {
char* tmp = ARENA_ALLOC_N(arena, char, 256);
} // auto cleanupI like this pattern because it gives temporary allocations lexical scope. Any allocation made inside the block is automatically reclaimed, even if the function has multiple return paths or exits early.
It feels similar to RAII or defer, while remaining standard C. The only cost is saving and restoring a single arena index.
I'm curious whether this pattern is common in C codebases. Have you used something similar, or do you prefer explicit arena_reset() calls?
•
u/AutoModerator Jul 16 '26
Hi /u/swe__wannabe,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
20
u/latkde Jul 16 '26 edited Jul 16 '26
This is simply not true, and your test suite doesn't demonstrate this scenario. In current standard C, there is no way to do scope based resource management when there's divergent control flow. We'll have to wait for C2Y, which specifies a
deferkeyword addressing exactly this use case. (Or just use C++, where we can use destructors.)The best resource management pattern I know in standard C is to stick to a single return and use
goto cleanupin place of early returns.5
u/WittyStick Jul 16 '26 edited Jul 16 '26
This pattern isn't very common, but is known and has been used in various libraries. Clay (UI layout library) for example uses it for their
CLAYmacro, for more than just deallocation, but because it is a way to implement "reverse breadth first" iteration which is needed for some layout calculations.There are of course issues with it - if you use any non structural branching (
return/break/goto) inside the secondary block it won't callarena_scratch_endand you will leak memory.It is more common to use GCC's
cleanupattribute on the variable, in lieu ofdeferwhich is not yet standardized. This will force a destructor to be called when the variable loses scope, and will always be called regardless of whether there's an earlyreturnor not.2
u/mivanchev Jul 16 '26
This can be done much easier using the cleanup attribute combined with nested functions.
2
u/mlugo02 Jul 18 '26
I love me some temporary memory regions