[Cython] Proposal: idea for automatic management of dynamic memory

Stefan Behnel stefan_ml at behnel.de
Fri Apr 11 10:12:33 CEST 2008


Hi,

Brian Granger wrote:
> In a function it is often necessary to allocate dynamic memory.  It is
> desirable to have something that it 1) fast and 2) doesn't lead to
> memory leaks if something goes wrong and 3) is easy to debug.
> 
> Proposal
> -------------
> 
> The main idea (Lisandro came up with this) is to build these
> capabilities into cython itself by introducing the following syntax:
> 
> cdef foo(size):
>   cdef double a[size]    # This is like the dynamic arrays in C99
>   # do stuff with a, but don't worry about deallocating the memory!!!

I actually like the syntax as it is rather clear that this is memory local to
the function. It's just like having it allocated on the stack, with the
exception that it depends on run-time state. So the automatic deallocation at
function exit is not unexpected.

Regarding PyMalloc, I think it's a good idea to use it, but you have to take
care in "nogil" functions. They require plain malloc()/free() calls.

Stefan


More information about the Cython-dev mailing list