Search code examples
cdynamic-memory-allocationstatic-analysisstatic-memory-allocationautomatic-storage

Absolute worst case stack size based on automatic varaibles


In a C99 program, under the (theoretical) assumption that I'm not using variable-length arrays, and each of my automatic variables can only exist once at a time in the whole stack (by forbidding circular function calls and explicit recursion), if I sum up all the space they are consuming, could I declare that this is the maximal stack size that can ever happen?

A bit of context here: I told a friend that I wrote a program not using dynamic memory allocation ("malloc") and allocate all memory static (by modeling all my state variables in a struct, which I then declared global). He then told me that if I'm using automatic variables, I still make use of dynamic memory. I argued that my automatic variables are not state variables but control variables, so my program is still to be considered static. We then discussed that there has to be a way to make a statement about the absolute worst-case behaviour about my program, so I came up with the above question.

Bonus question: If the assumptions above hold, I could simply declare all automatic variables static and would end up with a "truly" static program?


Solution

  • Even if array sizes are constant a C implementation could allocate arrays and even structures dynamically. I'm not aware of any that do (anyone) and it would appear quite unhelpful. But the C Standard doesn't make such guarantees.

    There is also (almost certainly) some further overhead in the stack frame (the data added to the stack on call and released on return). You would need to declare all your functions as taking no parameters and returning void to ensure no program variables in the stack. Finally the 'return address' of where execution of a function is to continue after return is pushed onto the stack (at least logically).

    So having removed all parameters, automatic variables and return values to you 'state' struct there will still be something going on to the stack - probably.

    I say probably because I'm aware of a (non-standard) embedded C compiler that forbids recursion that can determine the maximum size of the stack by examining the call tree of the whole program and identify the call chain that reaches the peek size of the stack.

    You could achieve this a monstrous pile of goto statements (some conditional where a functon is logically called from two places or by duplicating code.

    It's often important in embedded code on devices with tiny memory to avoid any dynamic memory allocation and know that any 'stack-space' will never overflow.

    I'm happy this is a theoretical discussion. What you suggest is a mad way to write code and would throw away most of (ultimately limited) services C provides to infrastructure of procedural coding (pretty much the call stack)

    Footnote: See the comment below about the 8-bit PIC architecture.