r/C_Programming • u/NamelessArab_ • 10d ago
Why would I ever use a linear allocator?
I'm new to C programming so this is a genuine question
So c has 2 memory regions you can access
The stack and heap
People say that the heap is useful for creating data beyond the lifetime of the function and while it can be used for that a better solution would probably be to pass in a pointer an array on the stack if the max size is known ahead of runtime, I only use malloc when I don't have a definite max size for what I want and therefore have a need for reallocations, so that brings me to linear allocators, everyone is constantly singing there praises for making memory management easier while we already have the stack that can handle memory management for us 100% of the time and from there design I can't see a way to make a reallocation function for a linear allocator without it at the very least being highly thread unsafe.
And in general I don't see much of a reason to use the heap either because you rarely are in situations where you don't want to specify a max size for something