|
From: | Kristian Lein-Mathisen |
Subject: | Re: [Chicken-users] Chicken C interface |
Date: | Thu, 6 Jun 2013 11:46:28 +0200 |
>> [...]
>> 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.
_______________________________________________
Chicken-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users
[Prev in Thread] | Current Thread | [Next in Thread] |