Is there a way I can measure how much stack memory a function uses?
This question isn't specific to recursive functions; however I was interested to know how much stack memory a function called recursively would take.
I was interested to optimize the function for stack memory usage; however, without knowing what optimizations the compiler is already making, it's just guess-work if this is making real improvements or not.
To be clear, this is not a question about how to optimize for better stack usage
So is there some reliable way to find out how much stack memory a function uses in C?
Note: Assuming it's not using alloca or variable-length arrays,
it should be possible to find this at compile time.
-Soption to generate an assembly file from your .c file, which you can examine with any text editor. The other option is to use a debugger that shows you the assembly code. That way, you can step through the code and see how the stack pointer and base pointer are used.-fstack-usageflag - you'll have to calculate the usage of the call-graph yourself though (such as if the function is recursive, multiply it with the number of recursions.)