赛顿libcpp.vector递归过程中的分段错误

2024-04-29 17:18:08 发布

您现在位置:Python中文网/ 问答频道 /正文

vectorduring a recursion to从类中推送一些实例,但不幸的是,我得到了一个分段错误。在

下面是一个简化版本,其中也会发生此错误:

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文件

^{pr2}$

在设置.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错误:非常奇怪


Tags: fromselfcountersimplegridintvectortp