Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Mar 29, 2016 at 12:21 UTC ( [id://1159003]=note: print w/replies, xml ) Need Help??


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

return the array

You cannot return an array from a C function. Your c code example is returning the modified value of the first element of the array passed (by reference).

That means that in the following C calling code:

... double rv = 0, ary[ 10 ] = { 0, }; rv = do_nothing( ary, sizeof( ary );

After the call, rv would contain 1.0, the modified value of ary 0 ; and ary would have been modified to contain 10 x 1.0.

So what do you want the function -- as seen from Perl - to do?

  • Take a reference to an existing perl array and modify it in place?
  • Take a reference to an array and return a list of values derived from the contents of the passed array and modified by the C function?
  • Take a list of values, and return a list of modified values?
  • Take a reference to an array and return a reference to a new array containing the modified values?

All of these are possible, but only the first can be written in C alone; all the others would need to be written in XS code.


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.

Replies are listed 'Best First'.
Re^2: perl XS - passing array to C and getting it back
by kopolov (Acolyte) on Mar 29, 2016 at 12:44 UTC
    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.
      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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found