For most things that Perl does with strings, it will want the string in a scalar, which implies that the string is in a buffer that Perl allocated. So most ways that you turn a C char * into a Perl string involve allocating a new buffer (often using strlen() to determine the size) and then copying the entire string into the new buffer.

The problem with this usually isn't running out of memory (since the space is usually reused if you do this over and over), but that all of the copying gets slow.

My personal preference when dealing with large strings going between Perl and C is to have Perl allocate a big scalar and put the string in there and then have C deal with that copy of the string. This is because Perl nearly refuses to even look at strings if they aren't in buffers that it allocated. [ About the only thing Perl can do with other strings involves using several iterations of pack() and unpack() to copy parts of the string into Perl's own buffers or just passing the pointer around to other C code. ]

Usually the C APIs don't have much problem with this. Rarely you find C code that insists on allocating its own buffers and also refuses to deal properly with a moved copy of the data.

Exactly how to allocate this SV to hold your string depends on, well, the details, which you didn't give. Feel free to post details if you have problems there.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Passing reference to perl from C code by tye
in thread Passing reference to perl from C code by Anonymous Monk

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.