In XS, when using sv_setpv or sv_setpvn, what happens to the char pointer? will perl create a SV around the pointer/string, and then free it eventually through normal GC? or will perl copy the contents/string that the pointer points to into its own new allocated memory range that makes up the SV and then its my responsibility to free the pointer manually or through the C block ending?

Similar question for SvPV, I get a pointer from perl, who is supposed to free this pointer? will perl GC/free it when the scalar goes out of scope? is this the pointer of the actual data inside the SV or a newly allocated memory range with the bytes copied from the SV to the new memory range? if I change the data at SvPV's pointer, will the SV be updated in perl land automatically?

What is the SVPVX pointer?

How is SvPV_set different from sv_setpv?

Does perl every "defragment" its SVs which would invalidated any pointers collected from SvPV or SvPVX?

How stable are pack()'s pointers? When does perl free or kill them? What happens if the scalar string in the scalar that pack gave a pointer to shrinks or expands, from Perl land, will the pointer from pack be still valid/the same?

Perl offers Newx and Safefree. How safe to interchange these 2 with native C library free and malloc? Can a pointer made with 1 be freed with the other or vice versa? More specifically on ActivePerl. Are there any cases when you want to use native C library memory management instead of perl memory management or vice versa?

In reply to XS, pointer scope, strings, allocation responsibility, sv_setpv, SvPV, pack's pointer by patcat88

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.