Home > Enterprise >  -Wstack-usage=byte-size in GCC
-Wstack-usage=byte-size in GCC

Time:12-26

The above-mentioned GCC flag has caused some confusion for me.
Here it says the following:

-Wstack-usage=byte-size

Warn if the stack usage of a function might exceed byte-size. The computation done to determine the stack usage is conservative. Any space allocated via alloca, variable-length arrays, or related constructs is included by the compiler when determining whether or not to issue a warning.

So what does "The computation done to determine the stack usage is conservative." mean?

I linked a small program written in C and intentionally used -Wstack-usage=1 to see the warnings and stack usages for various functions.

A few of the warning messages can be seen below:

Util.cpp: In function 'getCharInput.constprop':
Util.cpp:113:6: warning: stack usage is 64 bytes [-Wstack-usage=]
  113 | void util::getCharInput( char* const inputBuffer, const std::streamsize streamSize )
      |      ^
Main.cpp: In function 'main':
Main.cpp:10:5: warning: stack usage is 112 bytes [-Wstack-usage=]
   10 | int main( )
      |     ^

Why the stack usage of main is only 112 bytes despite that it calls all the other functions? Doesn't it keep the callee on its stack frame until the callee returns and gets deleted from the stack frame of main? I might have the wrong knowledge though.

CodePudding user response:

Why the stack usage of main is only 112 bytes despite that it calls all the other functions?

Stack usage is calculated by GCC is for this function only. This is also in the documentation: "Warn if the stack usage of a function might exceed byte-size".

Doesn't it keep the callee on its stack frame until the callee returns and gets deleted from the stack frame of main?

Yes, so when executing that code that happens. GCC does not statically traverse the whole call stack. It just calculates stack usage just for one specific function and checks if the usage of that single specific functions is greater than some threshold.

  • Related