是否会由于内存有限而导致python分段错误?

是否会由于内存有限而导致python分段错误?,python,python-2.7,error-handling,segmentation-fault,Python,Python 2.7,Error Handling,Segmentation Fault,今天我在解决一个关于黑客等级的问题:糖果。 我尝试了一种递归算法。 除一个测试用例外,所有测试用例都通过了。显示运行时错误。所以我下载了输入文件并在我的代码上运行它 import sys sys.setrecursionlimit(10000000) # Enter your code here. Read input from STDIN. Print output to STDOUT class Node : def __init__(self,imp): s

今天我在解决一个关于黑客等级的问题:糖果。 我尝试了一种递归算法。 除一个测试用例外,所有测试用例都通过了。显示运行时错误。所以我下载了输入文件并在我的代码上运行它

import sys
sys.setrecursionlimit(10000000)

# Enter your code here. Read input from STDIN. Print output to STDOUT

class Node :

    def __init__(self,imp):

        self.value = 0
        self.imp = imp
        self.left = self.right = None

    def get_val(self) :
        #print self.imp
        if self.value != 0 :
            return

        if not self.right and not self.left :
            self.value = 1
            return

        if not self.left :

            if self.right.imp >= self.imp :

                self.value = 1

            else :

                self.right.get_val()
                self.value = self.right.value + 1
            return

        if not self.right :
            if self.left.imp >= self.imp :
                self.value = 1
            else :
                self.value = self.left.value + 1

            return

        else :
            if self.left.imp == self.imp :
                if self.right.imp >= self.imp :
                    self.value = 1

                else :
                    self.right.get_val()
                    self.value = self.right.value + 1

            elif self.right.imp == self.imp :
                if self.left.imp > self.imp :
                    self.value =1 
                else :
                    self.value = self.left.value +1

            elif self.left.imp > self.imp <self.right.imp :
                self.value = 1
            elif self.left.imp < self.imp <self.right.imp :
                self.value = self.left.value +1

            elif self.left.imp > self.imp  >self.right.imp :
                self.right.get_val()
                self.value = self.right.value +1
            elif self.left.imp < self.imp  > self.right.imp :
                self.right.get_val()
                if self.left.value > self.right.value :
                    self.value = self.left.value +1
                else :
                    self.value = self.right.value +1





with open("input",'r') as textfile :
    a = int(textfile.readline().strip())
    start = Node(int(textfile.readline().strip()))

    temp1 = start

    for i in range(a -1 ) :
        temp = Node(int(textfile.readline().strip()))
        temp1.right = temp
        temp.left = temp1
        temp1 = temp

temp1 = start

for i in range(a) :
    print temp1.imp,i
    temp1.get_val()

    temp1 = temp1.right

print "done"
temp1 = start
toffee = 0
for i in range (a) :
    #print temp1.value
    toffee += temp1.value


    temp1 = temp1.right



print toffee
为了确切地了解发生这种情况的地方,我在第10行的代码中添加了注释掉的print语句(这基本上可以告诉递归调用在哪里)。产量是从100000到75064的一系列数字 然后是分割错误

算法的鸟瞰视图是Node(100000)。get_val()将调用Node(99999)。get_val()将依次调用Node(99998)。get_val()等等

因此,我的猜测是在100000-75064次调用达到内存限制后,它无法生成更多节点,因此调用分段错误。但是我读到python没有内存上限。那么为什么会发生这种情况呢

另外,我可以对我的代码做很多改进,使其更快,占用更少的内存,但这是我第一次在python中遇到分段错误,我真的很想知道为什么会发生这种情况。我以前在C++中处理过分段错误。但与他们相比,这一个似乎真的太离谱了


此外,我的代码以降序顺序运行于较小的输入。只有当我增加输入量时才会发生这种情况。

有一个理由说“这应该小心进行,因为过高的限制可能会导致崩溃。”
sys.setrecursionlimit(10000000)
没有小心进行。如果您遇到递归限制问题,
sys.setrecursionlimit
很少是解决方案-您应该更改程序以使用更少的堆栈空间。即使Python本身没有对内存施加上限,您的计算机也会这样做。计算机科学是对有限系统的研究。
Segmentation fault (core dumped)