perl.inline
[Top] [All Lists]

Re: Allocating a char* and returning to Perl from a C function

Subject: Re: Allocating a char* and returning to Perl from a C function
From: pfca@xxxxxxxxxxxxxxx (Paulo Filipe Andrade)
Date: Thu, 31 Jul 2008 14:14:26 +0100
Newsgroups: perl.inline



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@xxxxxxxxxx





<Prev in Thread] Current Thread [Next in Thread>