Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I track recursion depth?

Tags:

c++

I have a recursive function that I am troubleshooting. I want to output the recursion depth...i.e. how many times the function calls itself.

If I use a static variable that increments at the top of the function and decrements at the bottom.

Will this accurately give me the depth level?

recurse()
  {
  static int depth = 0;
  depth++;
  /*
  do stuff
  recurse()
  */
  depth--;
  return return_value;
  }
like image 533
user1001776 Avatar asked Oct 19 '11 23:10

user1001776


1 Answers

recurse(int depth = 0){
    ....
    recurse(depth+1);
}
like image 112
romaninsh Avatar answered Sep 18 '22 06:09

romaninsh