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

Bill Baxter wbaxter@gmail....
Sat Apr 21 18:22:08 CDT 2007


On 4/22/07, Travis Oliphant <oliphant.travis@ieee.org> wrote:
> Bill Baxter wrote:
> > What's the right way to make a new numpy array that's a copy of some C data?
> >
> 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?

Yes.

> 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.

Ok, I see.  That's not too hard.  Though as Sebastian said below it
would be a little trickier if I needed to do a transpose or something
at the same time.  I guess for those cases it should be possible to
make a temporary PyArrayObject with PyArray_NewFromDescr that wraps
the memory and then do a PyArray_GETCONTIGUOUS?

> > 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.

Hmm.  Sounds a little over my head.   I'll stick with copying for now.

--bb


More information about the Numpy-discussion mailing list