Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
We don't bite newbies here... much
 
PerlMonks  

Comment on

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

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (5)
    As of 2014-04-18 00:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (460 votes), past polls