Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

perl xs pass an array by reference

by andromedia33 (Novice)
on Dec 03, 2012 at 20:23 UTC ( #1006944=perlquestion: print w/replies, xml ) Need Help??
andromedia33 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, forgive me for asking a really basic question; I tried my best without getting anywhere with Perl XS, and I'm hoping someone could help me out on this.

I basically tried to pass an array by reference to my C subroutine that takes a pointer to a double. Here's the c code and header I have:

========my c file======
#include "myfunction.h" double do_nothing(double *para) { return para[0]; }
====end of c file=====
===my header file ===
double do_nothing(double *)
===end of header file==
===my xs file =======
#include "myfunction.h" (and the rest of the XSUB header files) MODULE = Myfunction PACKAGE = Myfunction PROTOTYPES: ENABLE double do_nothing(x) double * x
===============end of my xs file ======
=============my local typemap file============
double * T_SV
=============end of typemap file=============

when I ran "make" it seemed fine. but when I tried to test my code by including the following command in my test file:

my @array = (1.1,1.2); is(&myfunction::do_nothing(\@array),1.1);
it failed, saying:

got: '4.56890170385568e-317',
expected: '1.1'

What did I do wrong here? many thanks in advance!

Replies are listed 'Best First'.
Re: perl xs pass an array by reference
by bulk88 (Priest) on Dec 04, 2012 at 19:56 UTC
    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

      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);
        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006944]
Approved by kcott
Front-paged by bulk88
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2016-12-04 23:13 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (70 votes). Check out past polls.