[Numpy-discussion] Building an ndarray around an already allocated area of memory

James Dominy james at nbn.ac.za
Wed May 16 18:04:29 EDT 2007


Hi,

First time post, and pretty new to numpy. I'm wrapping another library (SUNDIALS) in python, using ctypes, and was wondering if
there were a way to create an ndarray python object around an array (ctypes.c_float*32, for example) without copying it?

For example, I have an array created using ctypes

----
import ctypes
import numpy

ctarray = (ctypes.c_float*32)()
----

And want to create an ndarray such that both the ndarray and the ctypes array are using the same actual memory area. The reason
for doing this this, is because using numpy methods to manipulate the contents of 'ctarray' may be useful to some people, and
the array should optimally be changed in place, rather than creating a copy as an ndarray, manipulating, then copying back. Of
course, if the ndarray object were garbage collected, the memory allocated for the actual array shouldn't be deallocated. Is
this possible?

Thanks,
James



More information about the NumPy-Discussion mailing list