Re: Returning a pointer to local data

2003-10-31 Thread Steve Hay
Tassilo von Parseval wrote: On Fri, Oct 31, 2003 at 12:39:14PM +1100 Sisyphus wrote: Steve Hay wrote: So I tried rewriting my original example like this: SV * hello() PREINIT: char*chartext; SV*svtext; CODE: Newz(0, chartext, 13, char); strcpy(chartext,

Re: Returning a pointer to local data

2003-10-31 Thread Tassilo von Parseval
On Fri, Oct 31, 2003 at 09:34:46AM + Steve Hay wrote: Tassilo von Parseval wrote: I get a segfault, which is what I would expect. You only allocate memory for the string but not for the SV to be returned. [snip] One could change it to: SV * hello() PREINIT:

Re: Returning a pointer to local data

2003-10-31 Thread Nick Ing-Simmons
Steve Hay [EMAIL PROTECTED] writes: I understand the second case there -- when the xsub is called as hello(1) -- but that leaves me wondering what ST(0) was in the case where the xsub was called without arguments. It is a stack - a hunk of memory. So if you don't have any args one it is: -

Re: Returning a pointer to local data

2003-10-31 Thread Steve Hay
Steve Hay wrote: At this point I have: void hello(...) PREINIT: char*chartext; CODE: Newz(0, chartext, 13, char); strcpy(chartext, Hello, world); ST(0) = newSV(0); sv_usepvn(ST(0), chartext, 13);

Re: Returning a pointer to local data

2003-10-31 Thread Nick Ing-Simmons
By the way what is the reason you do a PREINIT which does no init rather than CODE: { char *chartext; ... } ? I once fell foul of the problem described in the PREINIT entry in the perlxs manpage, namely: If a variable is declared inside a CODE: section it will follow any typemap code

Re: Returning a pointer to local data

2003-10-30 Thread Nick Ing-Simmons
Steve Hay [EMAIL PROTECTED] writes: What's the best way to have an XSUB return a string (char *) when the caller doesn't know how big that string will be? At a mortal string SV: void my_xsub() CODE: { ST(0) = sv_2mortal(newSVpv(string,strlen(string)); XSRETURN(1); } I like that style

Re: Returning a pointer to local data

2003-10-30 Thread Sisyphus
Nick Ing-Simmons wrote: SV * my_xsub() CODE: { RETVAL = sv_2mortal(newSVpv(string,strlen(string)); } OUTPUT: RETVAL As regards that, Nick - is the 'sv_2mortal' part necessary ? I thought *not* - but I'm sometimes wrong :-) Cheers, Rob -- Any emails containing attachments will be deleted from

Re: Returning a pointer to local data

2003-10-30 Thread Tassilo von Parseval
On Thu, Oct 30, 2003 at 08:21:38AM + Nick Ing-Simmons wrote: Steve Hay [EMAIL PROTECTED] writes: What's the best way to have an XSUB return a string (char *) when the caller doesn't know how big that string will be? At a mortal string SV: void my_xsub() CODE: { ST(0) =

Re: Returning a pointer to local data

2003-10-30 Thread Sisyphus
Tassilo von Parseval wrote: On Thu, Oct 30, 2003 at 08:02:53PM +1100 Sisyphus wrote: Nick Ing-Simmons wrote: SV * my_xsub() CODE: { RETVAL = sv_2mortal(newSVpv(string,strlen(string)); } OUTPUT: RETVAL As regards that, Nick - is the 'sv_2mortal' part necessary ? I thought *not* - but I'm

Re: Returning a pointer to local data

2003-10-30 Thread Nick Ing-Simmons
Tassilo Parseval [EMAIL PROTECTED] writes: Is this certain that my_xsub() returns a mortal? No - but it needs to return something which something will eventually free. When looking into the generated C code, I see: { char *RETVAL; RETVAL = string; sv_setpv(TARG, RETVAL);

Re: Returning a pointer to local data

2003-10-30 Thread Nick Ing-Simmons
Sisyphus [EMAIL PROTECTED] writes: Tassilo von Parseval wrote: On Thu, Oct 30, 2003 at 08:02:53PM +1100 Sisyphus wrote: Nick Ing-Simmons wrote: SV * my_xsub() CODE: { RETVAL = sv_2mortal(newSVpv(string,strlen(string)); } OUTPUT: RETVAL As regards that, Nick - is the 'sv_2mortal' part

Re: Returning a pointer to local data

2003-10-30 Thread Tassilo von Parseval
On Thu, Oct 30, 2003 at 11:13:47AM + Nick Ing-Simmons wrote: Sisyphus [EMAIL PROTECTED] writes: Tassilo von Parseval wrote: It is. If you have code like { my $var = my_xsub(); ... } and want $var to be cleaned up when it falls out of scope, you

Re: Returning a pointer to local data

2003-10-30 Thread Nick Ing-Simmons
Tassilo Parseval [EMAIL PROTECTED] writes: On Thu, Oct 30, 2003 at 11:13:47AM + Nick Ing-Simmons wrote: Sisyphus [EMAIL PROTECTED] writes: Tassilo von Parseval wrote: It is. If you have code like { my $var = my_xsub(); ... } Mortals are just

Re: Returning a pointer to local data

2003-10-30 Thread Sisyphus
Nick Ing-Simmons wrote: The sv_2mortal _IS_ needed in the void my_xsub() CODE: { ST(0) = sv_2mortal(newSVpv(string,strlen(string)); XSRETURN(1); } case. But it is inserted for you SV * return case: { RETVAL=newSV...; } #line 25 Foo.c ST(0) = RETVAL; sv_2mortal(ST(0)); }

Re: Returning a pointer to local data

2003-10-30 Thread Nick Ing-Simmons
Sisyphus [EMAIL PROTECTED] writes: Nick Ing-Simmons wrote: The sv_2mortal _IS_ needed in the void my_xsub() CODE: { ST(0) = sv_2mortal(newSVpv(string,strlen(string)); XSRETURN(1); } case. But it is inserted for you SV * return case: { RETVAL=newSV...; } #line 25 Foo.c ST(0) =

Re: Returning a pointer to local data

2003-10-30 Thread Dirk Koopman
On Thu, 2003-10-30 at 16:14, Steve Hay wrote: SV * hello() PREINIT: char*chartext; SV*svtext; CODE: Newz(0, chartext, 13, char); strcpy(chartext, Hello, world); sv_usepvn(svtext, chartext, 13); RETVAL = svtext;

Re: Returning a pointer to local data

2003-10-30 Thread Sisyphus
Steve Hay wrote: So I tried rewriting my original example like this: SV * hello() PREINIT: char*chartext; SV*svtext; CODE: Newz(0, chartext, 13, char); strcpy(chartext, Hello, world); sv_usepvn(svtext, chartext, 13); RETVAL = svtext;

Returning a pointer to local data

2003-10-29 Thread Steve Hay
What's the best way to have an XSUB return a string (char *) when the caller doesn't know how big that string will be? In C a function can't return a pointer to a function-static variable, but in Perl you can get away with a function returning a reference to a function-scoped lexical variable.

Re: Returning a pointer to local data

2003-10-29 Thread Sisyphus
Steve Hay wrote: What's the best way to have an XSUB return a string (char *) when the caller doesn't know how big that string will be? In C a function can't return a pointer to a function-static variable, but in Perl you can get away with a function returning a reference to a function-scoped