Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: perl xs pass an array by reference

by bulk88 (Priest)
on Dec 04, 2012 at 19:56 UTC ( [id://1007149]=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
Re^2: perl xs pass an array by reference
by andromedia33 (Novice) on Dec 06, 2012 at 20:48 UTC

    Thank you very much for your reply, bulk88! I was getting desperate. I really appreciate your help!

    You were right that I do need to pass the bulk of data and not just the 1st slice, and I need to do something in my C function and update values in the array that are passed as parameters. (so both in and out) In fact, my array is 2-dimensional, making the case more complicated...

    I tried your code (the second version) and everything works perfectly. In my actual application where I need the entire 2D array as input and output, do I need to do av_fecth(all indices) in order to access their addresses? Or is there a better way to do this? I realized that when I increment the address passed to my C function, I can't access the other elements in the array. For instance, if I were to do

    return para[1]
    in my C subroutine, it's returning the gibberish value again.

      Read Array Manipulation Functions. Use av_fetch and a for loop and i. Either use av_len once, or check the return of av_fetch for NULL before depointering. Perl's AVs are not C arrays. You must use the published function calls and macros for almost everything. Perl's AVs internally are C arrays of SV *s with metadata. So the return of av_fetch is not a double * but a SV **. If you want a C array of doubles, you have to build it yourself, it doesn't exist in the Perl engine. sv_newmortal a SV. Then do a "doublearr = sv_grow(mymortalsv, sizeof(double) * numofelements);" (not SvGROW, SvGROW will probably crash on you, I'm not explaining why right now) then do a loop using av_fetch and SvNV to fill doublearr. doublearr's memory will be freed when your XSUB returns since mymortalsv is "mortalized" and will free at the next perl lang scope or line change. The other choice is an algorithm that just uses av_fetch and SvNV and feeds doubles 1 by 1 to the C do_nothing, not keeping a C array of doubles around.
      return para[1]
      will never work. Is your XS code for yourself, or it is for others to see and use?

      A hackish, but fine but slower way of making an array of doubles is,
      $str = pack('ddd', 1.0, 1.1, 1.3);
      then
      void func(doublearrsv) SV * doublearrsv PREINIT: STRLEN len; double (*doublearr)[3];//I hope this works CODE: doublearr = SvPV(doublearrsv, len);//cast warning here if(len != sizeof(*doublearr)) croak("bad len"); do_nothing(&doublearr[2]); //THIS WILL UPDATE IN PERL LAND
      then after the XS call in perl
      @array = unpack('ddd', $str);
      An array of arrays, "$array13", is a SV reference to an AV, and each slice of the AV is a SV reference to another AV, which contains non reference SVs. Since you used AV * as a XS syntax prototype, the first SV reference you didn't see and it was automated away for you. If you do
      @arr = ([11,12,13], [21,22,23], [31,32,33]); myxsub(@arr);
      then you didn't get an AV or a SV reference. You would need a vararg XSUB.
      void myxsub(...) PREINIT: int i; CODE: for(i=0; i< items; i++){ SV * sv = ST(i); //do something }
      All code untested.

        thank you very much! that's exactly what i'm looking for! sorry it took me a long while to reply...i was fixing the c code part of the program. i really appreciate your help!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1007149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found