Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re^3: perl xs pass an array by reference by bulk88
in thread perl xs pass an array by reference by andromedia33

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2018-02-24 23:03 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (311 votes). Check out past polls.