LMPX.COM |
Home | Linux | Mysql | PHP | XML | ||
|
|
|||
From: Paulo Filipe Andrade Date: Thu Jul 31 07:14:26 2008 Subject: Re: Allocating a char* and returning to Perl from a C function
On Jul 31, 2008, at 1:41 PM, Sisyphus wrote: > SV * foo_3(int len) { > SV * outsv; > char c; > int i; > > outsv = NEWSV(0, len); > srand(time((time_t) NULL)); > > for(i = 0; i < len; ++i) > SvPVX(outsv)[i] = rand() % 256; > > SvPOK_on(outsv); > SvCUR_set(outsv, len); > *SvEND(outsv) = 0; > > return outsv; > } That's it. I got it working like the example above. I just have a couple of questions more about memory leaks. 1 - If you create an SV like so: outsv = newSV(lenX); and then use SvCUR_set to set the string length SvCUR_set(outsv, lenY); where lenY < lenX. When outsv is freed the whole buffer is still release right? SvCUR doesn't matter, right? 2- Finally, do I need to call sv_2mortal on outsv before returning it? Paulo F. Andrade pauloandrade@ist.utl.pt
| Navigate in group perl.inline at sever nntp.perl.org | |
| Previous | Next |
| © No Copyright You are free to use Anything |
Site Maintained by PHP Developer
Powered By PHP Consultants |