Perl supports the memory allocations systems other than malloc+free.
As such, memory that will be freed by Perl (such as a scalar's string buffer) will need to be allocated using the allocator Perl is configured to use. This is that to which Newx provides access. If you use an incompatible allocator, Perl won't be able to free the memory, and it might very well crash when it tries.
You can signal Perl not to free a scalar's string buffer by setting its SvLEN to zero. That way, you could assign malloc'ed data or even static-storage data (e.g. a constant) to a scalar. It becomes your responsibility to free it (e.g. in a the destructor of the object the contains the scalar). Note that setting SvLEN to zero won't stop Perl from replacing the buffer with a new one if do something like $s .= "!";.
In reply to Re: What difference between malloc and Newx, how attach a C string to SV directly?
by ikegami
in thread What difference between malloc and Newx, how attach a C string to SV directly?
by xiaoyafeng
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |