Well, I'm wrapping wxSocket* for wxPerl. Take a look in this function. Is the wxSocketBase::Read, that write directly to a buffer point of a Perl Scalar (SV):
long wxSocketBase::Read( buf , size , leng = 0 ) SV* buf long size long leng CODE: // Tell that the scalar is string only (not integer, double, utf8. +..) SvPOK_only(buf) ; // Get the original size: int org_size = SvCUR(buf) ; // Grow the scalar to receive the data and return a char* point: char* buffer = SvGROW( buf , size + 1 ) ; // Get the char* point of the end if you want to append data (len +> 0): if ( leng > 0 ) { buffer = SvEND(buf) ;} else { org_size = 0 ;} THIS->Read( buffer , size ) ; // Get the amount of data read in the socket: int nread = THIS->LastCount() ; if ( nread > 0 ) { // null-terminate the buffer, not necessary, but does not hurt buffer[nread] = 0 ; // tell Perl how long the string is SvCUR_set( buf , org_size + nread ) ; } // Return the amount of data read, like Perl read(). RETVAL = nread ; OUTPUT: RETVAL
The good of this is that you can see how to write or append to a buffer. And is more Perl based.

Note that "SvPOK_only(buf)" is important! Since if your variable is set for integer, like this:

my $var = 123 ;
.. you can't use SvGROW, SvCUR and SvCUR_set!

Take a look in the POD perlapi and perlguts too.

Graciliano M. P.
"The creativity is the expression of the liberty".


In reply to Re: Perl XS: garbage-collecting my malloc'd buffer by gmpassos
in thread Perl XS: garbage-collecting my malloc'd buffer by edan

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, 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, 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.