Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: perl xs pass an array by reference

by bulk88 (Priest)
on Dec 06, 2012 at 22:05 UTC ( #1007661=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^4: perl xs pass an array by reference
by andromedia33 (Novice) on Dec 31, 2012 at 19:18 UTC

    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://1007661]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-05-23 00:39 GMT
Find Nodes?
    Voting Booth?