January 19, 2026, 4:36am 1
This is perhaps too vague a question, but it’s on how to approach memory and such in Zig.
My background/understanding is we have the stack where we’d usually store transient state. However, some state must be dynamically allocated. This can be either when the alliocation amount is unknown or when the lifetime of a resource isn’t bound to function scope. Hence why there is malloc in C. Malloc is a general purpose purpose allocator handling requesting and tracking memory from the OS, so the idea with custom allocators is to have an allocator that suites your use case. However, I’ve been unable to wrap my mind on how to properly do that.
In my mind, I’m either going to go exact on memory or I’m going to at least have an upper bound. E.g., if my problem …
January 19, 2026, 4:36am 1
This is perhaps too vague a question, but it’s on how to approach memory and such in Zig.
My background/understanding is we have the stack where we’d usually store transient state. However, some state must be dynamically allocated. This can be either when the alliocation amount is unknown or when the lifetime of a resource isn’t bound to function scope. Hence why there is malloc in C. Malloc is a general purpose purpose allocator handling requesting and tracking memory from the OS, so the idea with custom allocators is to have an allocator that suites your use case. However, I’ve been unable to wrap my mind on how to properly do that.
In my mind, I’m either going to go exact on memory or I’m going to at least have an upper bound. E.g., if my problem as a fixed memory size requirement, since CPU cycles are cheap, calculate out the maximum size required for memory and just allocate that amount at once.
Alternatively, if for some reason you can make some calculations so as to obtain an upper bound, yet further calculations would be prohibitively expensive, I could see getting the upper bound and allocating that.
Is that the normal approach most people do when writing Zig and all the low-level concerns, or is it fine to play more fast and loose with things as (sometimes?) the memory isn’t actually grabbed immediately? E.g., you have the general purpose allocator (GPA) and people just alloc away.
What caused my question is that, doing what comes to mind for most memory efficient, the “calculate it all out” beforehand, “feels” very ugly, but if that’s how it has to be, I’d accept that.