Passing an array to a C function (using SWIG)

Armin Steinhoff Armin at Steinhoff_de
Sun Aug 13 04:08:36 EDT 2000


In article <8n4i0g$1kq$1 at nnrp1.deja.com>, aiglauer says...
>
>Hi
>
>Sorry if this is a dumb question but I have RTFM'd a fair amount in the
>Embedding Guide and the SWIG manuals and segfaulted a lot more and
>can't get this figured out......
>
>I need to pass an array of floats (length only known at runtime) to a C
>function (Calculation intensive recursive algorithm which is too slow
>for me in Python) and then return an array of floats.
>
>C Function definition:
>float *panjer(float *probs, long maxTot)
>
>SWIG wraps all pointers as strings - but I can't find an explanation of
>how to actually get an array INTO the C function.  Can anyone help me
>here?  I could probably do away with SWIG as I only have the one
>function to worry about and could do this by hand - SWIG just seems
>very elegant.

Yes ... and it is in some cases just inefficient and useless :-)

Here is my proposal:

 - put the floats into a list object and pass it to the C ext. code
 - use the 'object protocol' in order to read the floats
   (http://www.python.org/doc/current/api/abstract.html)
 - allocate an array of floats at the C side 
 - read the floats from the list object ans store it into the C array
   (see above ..)
 - do your computation
 - store the results in a list object ... if neccessary.

 Regards

 Armin




More information about the Python-list mailing list