in reply to Passing reference to perl from C code
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")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: Passing reference to perl from C code
by Anonymous Monk on Dec 21, 2000 at 22:05 UTC | |
by tye (Sage) on Dec 21, 2000 at 22:10 UTC |