chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] ffi - the right way to return a list holding integer


From: foobar
Subject: Re: [Chicken-users] ffi - the right way to return a list holding integers and strings
Date: Fri, 06 Jul 2007 09:45:29 +0200
User-agent: Thunderbird 1.5.0.10 (X11/20070403)

felix winkelmann schrieb:
> On 7/4/07, foobar <address@hidden> wrote:
>> hi,
>>
>> as the title allready suggests i'm looking for a way to return a list
>> from within
>> a c-function. I know how to return a list, but what i don't know is how
>> to fill
>> some of the elements with strings. More importantly how does the
>> allocation-deallocation-
>> scheme look like in this situation?
>>
>> Here is a minimal example of what i want to do
>>
>> C_word return_a_list(){
>>      int a = 1;
>>      char *str = "just a test";
>>      /* now i need to make the string an object that can be carried
>>         around by the list. But how? C_string or C_string2? */
>>
>>     /* finally return the list with the ??? actually being something
>> reasonable*/
>>      C_return(C_h_list(2,C_fix(a),???);
>> }
>>
>>
>> How can this be done in a safe way?
>>
>
> You have to use storage that can be garbage collected, which means stack
> storage. I recommend "foreign-primitive" and use "C_alloc" to allocate
> the
> storage and "C_string2" to allocate the string.
>

Great thanks. Just to be sure i've done it correctly.
Here is an example on how i would return a list of one string element.

(define return-a-list-with-string-elt
  (foreign-primitive scheme-object ()
                   "char str[] = \"hallo\";"
                   "C_word *sstr = C_alloc(C_SIZEOF_STRING(strlen(str)));"
                   "C_return(C_h_list(1,C_string2(&sstr,str)));"))

This code works as expected.
It does hopefully allso not leak memory.

greets david





reply via email to

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