在读取输入文件时更新C数组

时间:2023-01-31 15:41:28

I am now using cython to read an input file, convert the string to int and store them in a c array (instead of a list) to save space. The code I have looks like this:

我现在使用cython读取输入文件,将字符串转换为int并将它们存储在c数组(而不是列表)中以节省空间。我的代码看起来像这样:

cdef long p[10000000]
cdef long i
i = 0
f = open(filename, 'r')
for line in f:
    temp = map(int, line.split())
    p[i] = temp[0]
    i = i + 1
f.close()

However, the program is always aborted when I refer to the array p. Somehow the array is not "defined" as the memory usage is very low. It works, however, if I'm doing

但是,当我引用数组p时,程序总是被中止。不知何故,数组未被“定义”,因为内存使用率非常低。但是,如果我正在做的话,它会起作用

cdef i
for i in range(0, 1000):
    p[i] = i

1 个解决方案

#1


1  

My guesses:

  • the code you posted is actually wrapped in a function, in which case p is allocated on the stack and as soon as the given function returns, access to p is illegal.
  • 你发布的代码实际上包含在一个函数中,在这种情况下p被分配在堆栈上,一旦给定的函数返回,访问p是非法的。

  • you don't check i for overflow, what happens if i > 1000000?
  • 你不检查我是否溢出,如果我> 1000000会怎么样?

  • trying to allocate 1M 8-byte integers on stack may be beyond what is allowed, check ulimit -a
  • 尝试在堆栈上分配1M的8字节整数可能超出允许范围,检查ulimit -a

Overall there is not enough information in the OP, e.g.:

总体而言,OP中没有足够的信息,例如:

  • is that code top-level in module or content of a function?
  • 是函数模块或内容中的*代码?

  • how is program aborted (SEGV?)
  • 程序如何中止(SEGV?)

  • referring to p in what context?
  • 在什么情况下提到p?

  • what os/arch do you use?
  • 你用什么os / arch?

I could not reproduce your problem with Python 2.7.3 Cython 0.17.2 gcc 4.7.2 linux 3.6.9 x86-64

我无法用Python 2.7.3 Cython 0.17.2 gcc 4.7.2 linux 3.6.9 x86-64重现你的问题

#1


1  

My guesses:

  • the code you posted is actually wrapped in a function, in which case p is allocated on the stack and as soon as the given function returns, access to p is illegal.
  • 你发布的代码实际上包含在一个函数中,在这种情况下p被分配在堆栈上,一旦给定的函数返回,访问p是非法的。

  • you don't check i for overflow, what happens if i > 1000000?
  • 你不检查我是否溢出,如果我> 1000000会怎么样?

  • trying to allocate 1M 8-byte integers on stack may be beyond what is allowed, check ulimit -a
  • 尝试在堆栈上分配1M的8字节整数可能超出允许范围,检查ulimit -a

Overall there is not enough information in the OP, e.g.:

总体而言,OP中没有足够的信息,例如:

  • is that code top-level in module or content of a function?
  • 是函数模块或内容中的*代码?

  • how is program aborted (SEGV?)
  • 程序如何中止(SEGV?)

  • referring to p in what context?
  • 在什么情况下提到p?

  • what os/arch do you use?
  • 你用什么os / arch?

I could not reproduce your problem with Python 2.7.3 Cython 0.17.2 gcc 4.7.2 linux 3.6.9 x86-64

我无法用Python 2.7.3 Cython 0.17.2 gcc 4.7.2 linux 3.6.9 x86-64重现你的问题