Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cython libcpp.递归过程中的向量分段错误_Python_Cython - Fatal编程技术网

Python cython libcpp.递归过程中的向量分段错误

Python cython libcpp.递归过程中的向量分段错误,python,cython,Python,Cython,vector在递归到push_back`类中的一些实例的过程中,但不幸的是,我得到了一个分段错误 以下是出现此错误的简化版本: pyx文件 from libcpp.vector cimport vector from cython.operator cimport dereference as deref, preincrement as inc cdef class Grid: cdef unsigned int srow cdef unsigned int erow

vector
在递归到
push_back`类中的一些实例的过程中,但不幸的是,我得到了一个分段错误

以下是出现此错误的简化版本:

pyx文件

from libcpp.vector cimport vector
from cython.operator cimport dereference as deref, preincrement as inc


cdef class Grid:
    cdef unsigned int srow
    cdef unsigned int erow
    cdef unsigned int scol
    cdef unsigned int ecol

    def __init__(self, unsigned int srow, unsigned int erow, unsigned int scol,
            unsigned int ecol):
        self.srow = srow
        self.erow = erow
        self.scol = scol
        self.ecol = ecol



cdef simple_function_c(vector[Grid] &vp , unsigned int counter):

    cdef Grid p

    if counter == 10:
        return

    p = Grid(counter-1, counter, counter-1 , counter+1)
    vp.push_back(p)
    counter += 1
    simple_function_c(vp, counter)

def simple_function():

    cdef vector[Grid] vp
    cdef unsigned int counter
    cdef Grid tp

    counter = 0
    simple_function_c(vp, counter)

    print vp.size() #this works and outputs 10

    cdef vector[Grid].iterator it = vp.begin()
    while it != vp.end():
        tp =  deref(it) #Seg FAUL !!!
        print tp.srow, tp.erow, tp.scol, tp.ecol
        inc(it)
py文件

from testrec import simple_function
simple_function()
setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(ext_modules=[Extension(
                   "testrec",
                   ["testrec.pyx"],
                   language="c++")],
      cmdclass={'build_ext': build_ext})

我不知道为什么会这样。我注意到,当类只有两个字段时,我没有得到seg错误:非常奇怪更新:修复只需执行即可

cdef struct Grid:
     unsigned int srow
     unsigned int erow
     unsigned int scol
     unsigned int ecol