chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Chicken C interface


From: Thomas Chust
Subject: Re: [Chicken-users] Chicken C interface
Date: Thu, 06 Jun 2013 11:34:40 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6

On 2013-06-05 23:36, Felix wrote:
> From: Dan Leslie <address@hidden>
>> [...]
>> Basically, use C_alloc to allocate the memory required to host both
>> the List structure and the data it is to contain, then use the C_list
>> macro to patch it all together.
> 
> Note that this code is not correct: C_alloc allocates on the C stack and the
> data will be invalid once the function returns (Sorry). If this works, then
> it is just coincidental!
> [...]

Hello,

when I first saw that code I thought that this must be incorrect, too.
Then I checked the CHICKEN documentation for foreign-safe-lambda and read:

  "This is similar to foreign-lambda, but also allows the called
   function to call Scheme functions and allocate Scheme data-objects."

Now I'm confused. Of course C_alloc allocates on the stack and of course
this can likely break if a function just returns some pointer to stack
allocated data. However C_return could magically copy the return value
to the second generation heap or similar trickery to actually make
foreign-safe-lambda and C_alloc interoperate correctly or one could
perhaps use a special call ABI to prevent stack corruption upon return
from a foreign-safe-lambda and salvage stack allocated objects. Is any
such strategy actually implemented?

But maybe the documentation is just misleading and wanted to say
something about temporary allocation for the lifetime of the function
instead.

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]