Search code examples
clinuxstackdynamic-memory-allocation

Can we allocate Stack Memory at Runtime?


When a function recursively calls itself and stores the previous state in the stack, can we call the memory as runtime allocated? These call stack memory are not allocated at compile time, so is it safe to conclude that we can allocate stack memory at runtime?

I'm a bit confused by materials on the web that state Runtime Memory Allocation is the other name of Dynamic Memory Allocation, which I think is oversimplification.


Solution

  • Stack memory is always run-time allocated, but the C language specifically does not mention how/where allocation is done. And consequently C doesn't let us manipulate the stack directly, which is a good thing.

    Some non-standard functions like alloca may allow stack allocation. It is also very likely that variable-length arrays (VLA) are stack allocated. In either case, the lib or compiler handles the allocation/deallocation, like with any stack memory.

    "Runtime memory allocation" and even "dynamic memory allocation" are not formal terms, so they could mean anything. Although dynamic memory allocation is a common de facto standard name for allocated storage/free store/heap allocation... or whatever else it might be called. The formal term used by the C standard is allocated storage.