in reply to Re: perl XS - passing array to C and getting it back
in thread perl XS - passing array to C and getting it back

You are correct. I need the first option. (The return value is just for testing purposes). Pass array into C code which will manipulate its values. and then I want to be able to read those values in perl.
  • Comment on Re^2: perl XS - passing array to C and getting it back

Replies are listed 'Best First'.
Re^3: perl XS - passing array to C and getting it back
by BrowserUk (Patriarch) on Mar 29, 2016 at 13:03 UTC
    I need the first option.

    Then do_nothing1() below will get you started:

    #! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_1159002', CLEAN_AFTER_BUILD => 0 +; #define IS_VARS Inline_Stack_Vars #define IS_RESET Inline_Stack_Reset #define IS_ITEMS Inline_Stack_Items #define IS_ITEM( n ) Inline_Stack_Item( n ) #define IS_PUSHS( s ) Inline_Stack_Push( sv_2mortal( s ) ) #define IS_PUSHIV( iv ) Inline_Stack_Push( sv_2mortal( newSViv( iv ) ) + ) #define IS_PUSHUV( uv ) Inline_Stack_Push( sv_2mortal( newSVuv( uv ) ) + ) #define IS_PUSHNV( nv ) Inline_Stack_Push( sv_2mortal( newSVnv( nv ) ) + ) #define IS_DONE Inline_Stack_Done double do_nothing1( AV *x, int sizeOfX ) { int index; for( index=0; index<sizeOfX; index++ ) { av_store( x, index, newSVnv( 1.0 ) ); } return SvNV( *av_fetch( x, 0, NULL ) ); } void do_nothing2( AV *a, SV *size ) { IS_VARS; int index; IS_RESET; for( index = 0; index < SvIV( size ); ++index ) { IS_PUSHNV( 1.0 ); } IS_DONE; } void do_nothing3( SV*dummy, ... ) { IS_VARS; int index; for( index = 0; index < IS_ITEMS; ++index ) { IS_ITEM( index ) = newSVnv( 1.0 ); } return; } AV *do_nothing4( AV *x ) { IS_VARS; AV *r = newAV(); int index; for( index = 0; index < IS_ITEMS; ++index ) { av_push( r, newSVnv( SvNV( *av_fetch( x, index, NULL ) ) + 1.0 + ) ); } return r; } END_C my @a = ( 0 ) x 10; print do_nothing1( \@a, 10 ); print "@a"; my @b = ( 0 ) x 10; print join ' ', do_nothing2( \@b, 10 ); print join ' ', do_nothing3( 1 .. 10 ); my @c = ( 1 .. 10 ); print join ' ', do_nothing4( \@c );

    If you have Inline::C installed and you run the above code you'll get this output:

    C:\test>1159002.pl 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    If you then look in the _inline directory tree created below the cwd where you run this, and look for the file _Inline/build/_1159002/_1159002.xs you'll see how it all fits together:


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Assume I am really really (!!!) dumb. I can't use inline at all. It has to be perl XSUB which calls C function and pass it an array. can it be done?

        The easy approach is to install and use Inline::C to generate the XS scaffolding for your code. After that, you throw away Inline::C and only use its output.

        If you don't know how to install Inline::C, the easy approach is outlined in the following steps:

        1. Install local::lib (because you don't want to pollute the system Perl)
        2. Set your environment variables like local::lib proposes
        3. Download cpanm from http://cpanmin.us
        4. Run the following command:
          cpanm Inline::C

        You now have Inline::C and its prerequisites installed in a local directory and can use Inline::C to generate much of the XS code for you.

        I can't use inline at all.

        That's why I posted the .xs file generated by Inline::C. You'll need to grab Inline.h from the package and then the XS code I posted should compile directly.

        Of course, if you can compile XS code, there is no rational reason why you cannot use Inline::C; but if you prefer not to that's your choice.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.

        If you take the path of real XS code, be sure to use Devel::PPPort from the start. It'll save you a lot of headaches later.

        Then read the docs (twice):

        $ perldoc perlxs $ perldoc perlxstut $ perldoc perlxstypemap

        Enjoy, Have FUN! H.Merijn