Question

I have a function written in C which looks like this

void function1(){
    if(!some_condition){
        function1();
    }
}

This is compiled in gcc on a RedHat linux based machine. Is there a profiling tool that can tell me the maximum amount of stack space used by the code?

Was it helpful?

Solution

Massif from the valgrind suite should tell you this. However, you already know that your program is using the stack too extensively, so I'm not sure using it will help you much.

It seems to me you should refactor your code. Unfortunately, C is not Scheme, and the compilers are not required and don't do any tail recursion optimizations. So it may be a good idea to reorganize your function so it is looping instead of calling itself recursively. Here is a good read on Stacks and Recursion Elimination.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top