[SciPy-user] numpy array in ctype struct

Travis E. Oliphant oliphant@enthought....
Tue Jan 22 14:04:02 CST 2008


Rob Hetland wrote:
> On Jan 21, 2008, at 8:17 PM, Travis E. Oliphant wrote:
>
>   
>> You might find PyArray_AsCArray useful.
>>     
>
>
> Travis-
>
> Indeed, that was what I needed to get it to work right.  For  
> posterity, here is what I did.  I know I need a 2D array, so much of  
> the complexity in PyArray_AsCArray could be taken out.  I will also  
> assume a contiguous array, so that my function looks something like  
> this:
>
>
> void* function_wrap(double *pts1, int nPts, int dim) {
>
>      int i;
>      double **pts;
>
>      pts = (double **)malloc(nPts * sizeof(double *));
>      for (i=0; i<nPts; i++) {
>          pts[i] = pts1 + i*dim;
>      }
>
>      ///  Call and return result from some function that needs **pts
>
> }
>
>
>
> Are there any pitfalls to this approach?  It seems to work perfectly  
> for the test cases I have done.
>   
Some minor ones:

1) you have to remember to free the malloc'd pointers.
2) It is *not* the same as a 2-d static C array to the compiler which 
you would find if you tried to pass it
in to a subroutine that was (unfortunately) declared to need a specific 
size of 2-d array.

-Travis O.




More information about the SciPy-user mailing list