in reply to Re^4: XS efficient copy (SvCUR_set)
in thread XS efficient copy

The SvPV_nolen macro first checks the SV's private SVf_POK flag to see whether it contains a string. If it does, then it returns the pointer to the string, via SvPVX(SV*). If it doesn't, it calls sv_2pv_flags, which spits out a Use of uninitialized value warning, upgrades the sv, and returns (char*)"". That's safe to read from, but if you try to write to it... kaboom.
That was actually why I was calling SvPV_nolen (to force the SV to contain a string), but my understanding of it was a bit incorrect. I was expecting it to set that flag, but I was also expecting the return of the pointer to the existing buffer I had allocated with newSV, not an empty string.
When I realized that perl also had the length of the string (which, in retrospect, is obvious), the other part fell into place. I'm actually thinking that it IS safer to use SvPVX to get the buffer, so that I don't promote it 'by accident'.

Replies are listed 'Best First'.
Re^6: XS efficient copy (SvCUR_set)
by creamygoodness (Curate) on Apr 09, 2006 at 15:35 UTC