Chris Colbert skrev:
> Ok, I gotcha. My lack of formal programming training is on display today :)
>
> Thanks Sturla and Robert!
>
This is a very easy mistake to make, particularly if you are used to
working with Python or Java. Dangling pointers is (IMHO) one of the most
common C mistakes. Variable-length arrays in C99 makes this mistake even
easier to make.
In C++ you can avoid the problem using std::vector<> instead of local
arrays or new[] / delete[].
In Cython you can avoid the problem using numpy ndarrays instead of
local C arrays or malloc/free.
If you absolutely insist on using malloc/free in Cython, I suggest you
wrap it in a extension object that commit suicide on garbage collection.
Something like this (not tested):
cimport stdlib
cdef class mallocbuffer:
cdef readonly void *buf
cdef readonly size_t nbytes
def __cinit__(membuffer self, size_t nbytes):
self.buf = stdlib.malloc(nbytes)
if (self.buf == NULL):
raise MemoryError, 'malloc failed'
self.nbytes = nbytes
def __dealloc__(membuffer self):
if (self.buf): stdlib.free(self.buf)
Sturla Molden
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev