Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find maximum recursion depth

Tags:

c++

recursion

Is there a way to know in c++ the maximum recursion depth without explicitly calling the recursion until it crashes?

I've seen that's limited by the size of the stack. Maybe it can be useful to find at a specific recursion level the amount of free space in the stack. Is it possible?

like image 428
Jepessen Avatar asked Mar 01 '16 16:03

Jepessen


People also ask

How do you find the maximum recursion depth?

The recursion depth limit in Python is by default 1000 . You can change it using sys. setrecursionlimit() function.

How do you solve maximum recursion depth exceeded?

The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion limit. You can fix this error by rewriting your program to use an iterative approach or by increasing the recursion limit in Python.

What does maximum recursion depth mean?

sys.getrecursionlimit() Return the current value of the recursion limit, the maximum depth of the Python interpreter stack. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. It can be set by setrecursionlimit() .

What is the maximum depth of recursion function in Python?

Due to this, the recursion limit of python is usually set to a small value (approx, 10^4). This means that when you provide a large input to the recursive function, you will get an error. This is done to avoid a stack overflow. The Python interpreter limits the recursion limit so that infinite recursions are avoided.


2 Answers

The only thing that I can think of right now is to use getrlimit to get the maximum size of the stack dedicated to your process. Next thing to do is find a way of getting the currently used stack size. I thought that getrusage is the way to go but after looking at the man-page and a couple of posts on SO it seems that it no longer supports this particular feature. So you have to find another way. I do believe that Valgrind also reports the stack usage so looking into its source code and documentation may prove useful.

Once you are able to get the current stack size you can measure

  • its initial state before you start the recursion (so that you can exclude this from your calculations since it doesn't have anything to do with the recursion itself)

  • its change for a single iteration

Excluding the initial stack allocation along with using the total stack size and the allocation required for a single recursion step you should be able to approximate the number of recursions you can have for the given system. I'm not sure if it will work and also such measurements even if accurate are highly dependant on the system you are using (after all stack is closely related to the amount of virtual memory a process can have).

like image 188
rbaleksandar Avatar answered Oct 14 '22 03:10

rbaleksandar


The maximum recursion depth depends on the amount of memory used by the function(s), the amount of memory on your platform and the limits (if any) by the OS or the compiler.

In a recursive call, memory is occupied by:

  • The overhead of a function call
  • The memory occupied by the parameters passed.
  • The memory occupied by local variables

A recursive function with no parameters and no local variables will have a higher possible depth (number of recursive calls) than a function that passes a lot of large objects and occupies a lot of local variables.

So, the answer to your question is: the maximum number of recursive calls depends on the amount of memory occupied by a recursive call, the amount of memory on the system and any limits imposed by the compiler or operating system. Different recursive functions occupy different amounts of memory.

If you know all these items, then you can calculate the maximum number of possible recursions.

like image 34
Thomas Matthews Avatar answered Oct 14 '22 03:10

Thomas Matthews