http://www.perlmonks.org?node_id=1007149


in reply to perl xs pass an array by reference

double * T_SV
A SV * is not a double *. do_nothing is looking at garbage data (an SV struct), and you should have compiler warnings about the lack of a cast (and don't add one either). Look at lib/ExtUtils/typemap in perl.git. There is no typemap entry for a double *. Now, is do_nothing's x parameter an input or an output parameter?

I personally never use the automatic prototype mode of xsubpp, its not worth the learn curve, or discovering what its limitations are after you wrote the code, and then wind up having to write a PPCODE or CODE block anyway. I always write a PPCODE or CODE block. Here is your code (untested), assuming x is in and out. If you want only in, or only out, that is a little bit different.
MODULE = Myfunction PACKAGE = Myfunction PROTOTYPES: ENABLE double do_nothing(x) NV x CODE: RETVAL = do_nothing(&x); OUTPUT: RETVAL x
I basically tried to pass an array by reference to my C subroutine that takes a pointer to a double.


Why? An array is an aggregate, a pointer to a double is a singular item. Do you want to read only the first slice of the array and have that value be the double that is &ed, then passed to do_nothing (this is possible but makes little sense from the users aspect, api design wise)? Why would do_nothing the XSUB need to get an array ref and only see the first slice?

Some untested code to use an array ref, that must be 2 slices long, and the 1st ("[0]") slice is the double to use, and to later set. The return of av_fetch was not tested for null since an av_len check was done.
MODULE = Myfunction PACKAGE = Myfunction PROTOTYPES: ENABLE double do_nothing(x) AV * x PREINIT: SV ** svnv; double x_db; CODE: if(av_len(x) != 2) croak("bad array len"); svnv = av_fetch(x, 0, 0); x_db = SvNV(*svnv); RETVAL = do_nothing(&x_db); sv_setnv_mg(*svnv, (NV)x_db); OUTPUT: RETVAL