[Numpy-discussion] C-API creating new copy of C data

Travis Oliphant oliphant.travis at ieee.org
Sat Apr 21 15:58:29 EDT 2007


Bill Baxter wrote:
> What's the right way to make a new numpy array that's a copy of some C data?
>
> There doesn't seem to be any API like PyArray_NewFromDescr that
> /copies/ the void*data pointer for you.  Do I have to write my own
> loops for this?  I can do that, it just seems like it should be a
> library function already, so I'm guessing I'm just overlooking it.
> There seem to be lots of APIs that will wrap pre-existing memory, but
> the ones that allocate for you do not seem to copy.
>   

What do you mean by /copies/ the void * data pointer for you?   Do you 
mean the API would

1) Create new memory for the array
2) Copy the data from another void * pointer to the memory just created 
for the new array?

If that is what you mean, then you are right there is no such API.   I'm 
not sure that there needs to be one.  It is a two-liner using memcpy.

> A related question -- I'm only trying to copy in order to save myself
> a little hassle regarding how to clean up the allocated chunks.  If
> there's some simple way to trigger a particular deallocation function
> to be called at the right time, then that would be the ideal, really.
>   
No, there is no place to store that information in NumPy.  Either the 
ndarray dealloc function frees the memory it created or it doesn't free 
any memory.    I think the best thing to do in this case would be to 
create a memory object wrapping the pointer and then point the ndarray 
to it as the source of memory.

-Travis




More information about the NumPy-Discussion mailing list