Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a C array while reading an input file

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:

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

cdef i
for i in range(0, 1000):
    p[i] = i
like image 303
Eric YZ Avatar asked Nov 13 '22 20:11

Eric YZ


1 Answers

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.
  • you don't check i for overflow, what happens if i > 1000000?
  • trying to allocate 1M 8-byte integers on stack may be beyond what is allowed, check ulimit -a

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

  • is that code top-level in module or content of a function?
  • how is program aborted (SEGV?)
  • referring to p in what context?
  • what os/arch do you use?

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

like image 108
Dima Tisnek Avatar answered Nov 15 '22 12:11

Dima Tisnek