Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^2: perl xs pass an array by reference

by andromedia33 (Novice)
on Dec 06, 2012 at 20:48 UTC ( #1007648=note: print w/replies, xml ) Need Help??

in reply to Re: perl xs pass an array by reference
in thread perl xs pass an array by reference

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.

Replies are listed 'Best First'.
Re^3: perl xs pass an array by reference
by bulk88 (Priest) on Dec 06, 2012 at 22:05 UTC
    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);
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007648]
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-05-27 22:42 GMT
Find Nodes?
    Voting Booth?