SV * xs_new( ... ) CODE: size_t len= number_of_bytes_needed(); sv_grow( RETVAL, len ); // "malloc" void *buf= SvPV( RETVAL ); size_t used= constructor( buf, len, ... ); // Tell Perl how much of the buffer is actually in use: SvCUR_set( RETVAL, used );