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
I was initially a little concerned about there being both a Csub and a Perlsub named "do_nothing" but, of course, the Csub does not bind to Inline::C and the only sub named "do_nothing" that is visible from perl space is the Perlsub. Conversely, the only sub named "do_nothing" that is visible from XS space is the Csub.use strict; use warnings; use Inline C => Config => BUILD_NOISY => 1, USING => 'ParseRegExp', ; use Inline C => <<'EOC'; double do_nothing(double x[], int sizeOfX) { int index; for(index=0; index<sizeOfX; index++) x[index] = 1; return x[0]; } void do_nothing_c(SV * sv) { STRLEN len; char * pv; pv = SvPV_force(sv, len); /* SvPV() also seems ok */ do_nothing((double *)pv, len / sizeof(double)); } EOC my @in = (2.3, 3.4, 4.5, 5.6, 6.7); my @ret = do_nothing(@in); print "@in\n@ret\n"; sub do_nothing { my $doubles = pack 'd*', @_; do_nothing_c($doubles); return unpack 'd*', $doubles; } __END__ Outputs: 2.3 3.4 4.5 5.6 6.7 1 1 1 1 1
void do_nothing(double x[], int sizeOfX) { int index; for(index=0; index<sizeOfX; index++) x[index] = 1; }
|
|---|