[Numpy-discussion] ownership of numpy arrays

Travis Oliphant oliphant at ee.byu.edu
Wed Jan 17 10:47:44 EST 2007


josh kantor wrote:

>I am working on a project where I am doing some interfacing with numpy 
>arrays at a C level.
>
>One thing that I want to do (using pyrex) is to call numpy functions, then 
>directly access the underlying double * array of the resulting ndarray and 
>operate on it in C/pyrex (I don't want to copy it). 
>
A common need and easily done (though you need to take care to make sure 
you understand the general memory model for ndarray's or at least ensure 
you get an array following the memory model you are assuming). 

>I want to ensure that 
>the underlying double * wont be deallocated until I'm ready. I was going to 
>take the resulting ndarray and set the ownership bit of the result to 0 and 
>then I would be responsible for deallocating the memory when I was ready.
>
>Contrary to what I expected I noticed that it seems all numpy functions, for 
>example numpy.linalg.pinv as well as numpy.linalg.eig return matrices that 
>don't own their data.
>
>(If m is a numpy array)
>
>n=numpy.linalg.pinv(m)
>
>n.flags
>
>shows that n does not own its data.
>
>So I can't take their underlying data * with any certaintly that it won't be 
>deallocated before I'm ready. If n doesn't own its data, who does.  Is there 
>a safe way to do what I want?
>  
>

If you are holding a reference to the underlying array then it won't be 
de-allocated (even if it doesn't own it's own memory).

-Travis




More information about the NumPy-Discussion mailing list