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)


In reply to Re: perl guts and the SvPV() macro by derby
in thread perl guts and the SvPV() macro by mbr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.