r/cprogramming • u/JayDeesus • Jan 01 '26
Stack frame size
So I understand that the stack frame is per function call and gets pushed and popped once you enter the function and it all is part of the stack. The frame just contains enough for the local variables and such. I’m just curious, when does the size of the stack frame get determined? I always thought it was during compile time where it determines the stack frame size and does its optimizations but then I thought about VLA and this basically confuses me because then it’d have to be during run time unless it just depends on the compiler where it reserves a specific amount of space and just if it overflows then it errors. Or does the compiler calculate the stack frame and it can grow during run time aslong as there is still space on the stack?
So does the stack frame per function grow as needed until it exceeds the stack size or does the stack frame stay the same. The idea of VLA confuses me now.
7
u/stevevdvkpe Jan 01 '26
At the machine language level there is a stack pointer register that typically points to the most recently pushed element on the stack. A push adjusts the stack pointer by the size of an item to make room for it in the stack, then writes the pushed item at the new stack pointer location. A pop reads an item at the location of the stack pointer, then adjusts the stack pointer by the size of that item to release its stack space. Code can also add and subtract values from the stack pointer to reserve or release space on the stack at any time. Stack overflows may or may not be detected by software or hardware, so attempting to write into memory outside the range initially allocated to the stack may cause undefined behavior -- this may just overwrite other memory or trigger a segmentation fault in a virtual memory system, although sometimes the fault can be handled by changing the virtual memory allocation of the stack to expand it.
Typically functions also reserve space for local variables by adjusting the stack pointer by the aggregate size of all the local variables used in the function on function entry, and moving the stack pointer back on function exit. This can be determined at compile time so a constant for the stack adjustment is compiled into the code.
It's also possible for code to make other runtime changes to the stack, such as the C library function alloca() which allocates a requested amount of space on the stack and frees it when the function exits. alloca() can be called more than once in a function, so it has to track the total space that was dynamically allocated on the stack by all calls within the current function. Variable-Length Arrays can do similar dynamic allocation.
3
u/somewhereAtC Jan 01 '26
As you say, the frame has all the local variables, but if you call a function then the stack frame will grow for that function which might then call another function, and so on and so on. The needs of each individual function and the call tree can be calculated exactly unless recursion is in play which throws a wrench into the works. Then there are the interrupts which are basically separate threads on that same stack. Excluding recursion, though, the stack space is calculable.
At any moment the current size of the stack depends on probability. Does the big-hog function get called repeatedly? Are interrupts blocked for a few moments? If big_hog() only gets called when the serial bus gets data then the stack might be relatively short for a long period of time. The system designer has to have sufficient integrity to not play probability games.
The hidden gotcha is that malloc() usually competes for memory with the stack and memory is a finite resource. If the stack allocation is guaranteed and malloc() limits are guaranteed then no trouble. If the system designer is lazy then there could be a time when malloc( ) is over-borrowed at the moment that function big_hog() gets called. Since SP does not know where malloc() has grown, and the malloc() allocator doesn't check SP, it is entirely possible for sparks to fly.
Or you might load a library that uses recursion and ruin the calculus entirely.
2
u/aioeu Jan 01 '26
Yes, it's as simple as that. When variably-sized objects are allocated on the stack, the stack pointer will be adjusted accordingly.
This isn't too big a problem. Functions that use VLAs will generally keep using a base pointer, even if you have told the compiler to try to optimise those away, so BP-relative addressing means that all objects can still be easily located even as the SP is changed.
1
u/todo_code Jan 01 '26
From my understanding an activation record is really no different than the stack itself. it just push X number of bytes. with a variable length array, the same concept applies, it will probably push the count of length of the array onto the stack, and then the array onto the stack, and then call the function, so the function knows the length of the array. It's possible the activation record has a pointer to the array. I don't know this level of detail, and some compilers are probably different. so it would be something like 'elem 1 2 3 4' (4) (*elem)
1
u/ComradeGibbon Jan 01 '26
One thing to consider is that there is a base address of the stack frame. Classically that was stored in a register people refer to as a frame pointer.
But the end of the stack frame doesn't have to be known. As someone else said with a VLA or alloca() effect the size of the stack at run time.
1
u/dcpugalaxy Jan 01 '26
Normally it is determined at compile time but if you use a VLA or alloca it will be extended at runtime. It can grow at runtime (just do
addi sp,sp,64or whatever) but it just normally doesn't.1
u/tstanisl Jan 01 '26
Determining the stack size when variable-sized objects and/or arbitrary recursion is allowed is a non-decidable problem. Thus there are programs for which stack-consumption can not be determined by *any* compiler with *any* finite memory and time resources. Even limiting size of programs address space, disallowing VLAs and recursion, the problem is still NP-complete making solution intractable in practice. Just a upper-bounds are possible with exponentially growing proofs for the better bounds.
Typically, the stack size is determined in runtime when the process is run by operating system. On a modern system it is usually 1-8 MiBs but this can be adjusted. Note that this is refers only to reservation of an address space. The physical memory is allocated in chunks (aka pages) on the first write operation to a given chunk.
1
u/zhivago Jan 01 '26
Technically speaking, C has no stack.
C has auto storage and longjmp which make stacks a natural implementation choice.
But it means that your question must be about a specific C implementation rather than the language.
1
u/Bloopyhead Jan 02 '26
The stack frame is determined at compile time for each function for local variables and function parameters. The stack frame is set when you enter the function.
Typically it is always left unchanged…
…But!…
If you want it to, it can grow at runtime if you allocate memory on the stack using alloca(), which just moves the stack pointer down, it is way way faster than dynamic memory allocations like malloc(). Like, use this when you want to have a small-ish local but dynamically sized collection.
Obviously the pointer isn’t heap memory and DO NOT return it, since the contents get reused by other stack frames and will soon get corrupted.
Also be careful not to use it in a loop with or you may grow your stack size by a lot without knowing it.
Edit: someone else basically wrote what I wrote. So take that as the final answer.