Process finished with exit code -1073741571 (0xC00000FD) in Python

3

I am at early stage of learning Python. I tried calculating Ackerman function for smaller values. It worked just fine until the values are (3,7). Any value higher than that (say 3,8) throws this error. [Process finished with exit code -1073741571 (0xC00000FD)]

At first I checked whether recursion limit is reached, but the process stopped well below the set Recursion limit (in this case it is set to maximum)

import sys
sys.setrecursionlimit(999999999)
count = 0
def cf():
    global count
    count+=1
cf()
def Ack(m,n):
    if  m==0:
        x=n+1
        cf()
        return x
    elif m>0 and n==0:
        x=Ack(m-1,1)
        cf()
        return x
    elif m>0 and n>0:
        x=Ack(m-1,Ack(m,n-1))
        cf()
        return x
a,b=map(int,input("Enter values").split())
print(a,b)
result=Ack(a,b)
print(result)
print(count)
python
python-3.x
asked on Stack Overflow Jun 24, 2020 by Suresh Kumar

1 Answer

2

Simple as that, you are getting a stack overflow.

Recursion limit only dictates how deep the recursion calls can go, but it doesn't change the stack size. Each recursive call adds frames to the stack and eventually you are reaching the limit.

If you really want to go so deep with recursion, you have to change stack size with threading.stack_size() and create a new thread.

related question: Process finished with exit code -1073741571

answered on Stack Overflow Jun 24, 2020 by Jarek Piotrowski

User contributions licensed under CC BY-SA 3.0