[Numpy-discussion] wrapping question

Todd Miller jmiller at stsci.edu
Fri Oct 7 06:46:29 CDT 2005


usr01873 at prolocation.net wrote:

>Hi all,
>
>I'm using the numarray package. I would like to use the numarray C API to
>create a new array object and fill it with data without copying. Also, I
>  
>
The numarray C-API has a compatibility layer which supports most Numeric 
C-API functionality.   So,  using numarray,  you can write most 
Numeric/Numeric3/scipy_core-like code in C.  For historical reasons 
numarray also has a wider set of native APIs (i.e. NA_vNewArray()),  but 
since numarray is likely to be replaced by scipy_core when it matures,   
it's wisest to use the compatibility API.

>would like numarray to take care of freeing the data once the numarray
>object is destructed. I've managed to find some related posts in the
>mailing list archive, but it is really unclear which of these posts are
>still accurate and what the current 'recommended' approach is. See:
>
>http://sourceforge.net/mailarchive/message.php?msg_id=11788304
>http://sourceforge.net/mailarchive/message.php?msg_id=2494535
>
>Currently, I first create a new PyArrayObject that is large enough to hold
>the data:
>array = NA_vNewArray(NULL,__numarray_type,tmp_num_dims,tmp_dims);
>
>then I pass array->data to a C function that reads the data from disk. Any
>comments on this approach? 
>
That should work fine;  'array' owns the data and will free it when 
'array' is destructed. 

A better approach,  however,  is to use numarray's PyArray_FromDims() 
which is the Numeric/Numeric3/scipy_core API.  

>Also, I was wondering if numarray frees the
>data using this approach (because it also allocates the memory in the
>first place within the NA_vNewArray function.)
>  
>
Yes, numarray will free the data allocated internally by 
NA_vNewArray().  numarray will also free the data allocated by 
PyArray_FromDims().

Regards,
Todd




More information about the Numpy-discussion mailing list