mbr,
using a macro inside a function call is not considered
safe. From perlguts:
Also remember that C doesn't allow you to safely say
"foo(SvPV(s, len), len);". It might work with your com
piler, but it won't work for everyone. Break this sort of
statement up into separate assignments:
SV *s;
STRLEN len;
char * ptr;
ptr = SvPV(s, len);
foo(ptr, len);
And len is filled by SvPV and perl is pretty liberal when grabbing memory.
-derby
update Okay now that I read your question a little better ... when you do the NEWSV(0,16), you've basically preallocated the SV to hold upto 17 chars (16 + 1 for the null byte). SvPV will return the amount of space available in the SV (not the amount contained in the string). You're still better off separating the macro from the function and
then using the C pointer and system calls
char *foo = SvPV( RETVAL, 0 );
fcn( foo );
printf( "string: %s length: %d", foo, strlen(foo) );
And here's a bit of Inline showing that SvPV returns the
amount of allocated space, not the length of the contained
string (caution: very contrived):
#!/usr/bin/perl -w
use Inline C;
svfunc();
__END__
__C__
void svfunc( )
{
SV *val;
STRLEN x;
char *ptr;
ptr = malloc( 5 );
memset( ptr, 0, 5);
memset( ptr, 65, 4 );
val = newSVpv( ptr, 20 );
ptr = SvPV( val, x );
printf( "String is %s with len of %d (%d)\n",
ptr, x, strlen(ptr) );
}
yields: String is AAAA with len of 20 (4)
|