0

I've below function in driver:

function MyFilter( )
{
    bool PassThrough = true;

    if ( <some condition> )
    { 
         PassThrough = false;
    }  

    if ( PassThrough )
    {
        // some local stack variables  //Locals #1
        IofCallDriver( );
    }
    else 
    {
        // more local stack variables  //Locals #2
        NonPassThroughWork( );
    }
}

My query:

When above driver function is compiled (using MSFT C++ compiler), how much would be the stack consumption due to local variables? Would the local variable stack size of 'MyFilter' reflect the size due to the one defined in the inner scope (i.e., Locals #1, #2)?

YamHon.CHAN
  • 866
  • 4
  • 20
  • 36

2 Answers2

0

Probably best to check the generated assembly. I would have thought the worst case stack size taken up by locals is MAX(sizeof(#1),sizeof(#2)) + sizeof(PassThrough). Could be less though depending on optimizations. For example if PassThrough is not "live" after the first if it is used in then it might just reside in a register etc... same for other variable and register colouring. Perhaps compiler makes other optimizations too...

See the following for viewing the assembly

How to view the assembly behind the code using Visual C++?

I think going through the assembly will give you the most accurate answer to your question :)

Community
  • 1
  • 1
Jimbo
  • 4,352
  • 3
  • 27
  • 44
0

Stack space for local variables and everything else needed for function execution is reserved at compile time, hence whether or not this space is actually needed, i.e. the respective conditions evaluate to true, does not matter (and is actually not known at that point). The compiler typically allocates the complete memory space needed for a function's stack consumption at function entry including space for all local variables and space for function calls, i.e. input arguments, return address and return value.

Looking at the disassembled code should give you the same answer, you will usually find a single operation extending the stack frame by a specific number of bytes somewhere in the very beginning of a function. On a 32-bit machine using gcc it would be something like sub esp, 32.

D. Beck
  • 1
  • 3