[gambit-list] Returning struct as a value

Julian Scheid julians37 at googlemail.com
Fri Oct 9 06:23:54 EDT 2009


Hi,

I have the following two functions for converting a pointer to some
struct Foo into the "value" struct Foo:

(define value-Foo-1
  (c-lambda ((pointer (struct "Foo"))) (struct "Foo")
#<<c-lambda-end
void* mem = malloc(sizeof(Foo));
new (mem) Foo(*___arg1);
___result_voidstar = mem;
c-lambda-end
))

(define value-Foo-2
  (c-lambda ((pointer (struct "Foo"))) (struct "Foo")
    "___result = *___arg1;"))

(value-Foo-1) works and (value-Foo-2) crashes. Is this expected?

If so, is there a more elegant way to express the code of value-Foo-1?

Also: is it OK to use malloc here or am I supposed to use ___alloc_rc
or ___C_ASSIGN_NEW ?

Do I have to worry about providing code for releasing the memory allocated?

Finally, I am curious whether there is any difference internally
between the handling of (struct "Foo") and (type "Foo"), or is this
just naming for the sake of clarity?


More information about the Gambit-list mailing list