chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] return a pair of ints from a C function?


From: felix winkelmann
Subject: Re: [Chicken-users] return a pair of ints from a C function?
Date: Thu, 31 Jul 2008 22:39:49 +0200

On Thu, Jul 31, 2008 at 9:37 AM, Shawn Rutledge
<address@hidden> wrote:
> OK dumb question time again:  I'm trying to return a pair of fixnums
> from a C function.
>
> C_word g2d_glyphs_baseline(struct G2dGlyphs* glyphs)
> {
>        C_word* a = C_alloc(C_SIZEOF_PAIR + 8);
>        C_return(C_pair (&a, C_int_to_num(&a, 10), C_int_to_num(&a, 256)));
> }
>
> or maybe
>
> C_word g2d_glyphs_baseline(struct G2dGlyphs* glyphs)
> {
>        C_word* c_ret = C_alloc(C_SIZEOF_PAIR);
>        C_word* a = c_ret;
>        C_int_to_num(&a, 10);
>        C_int_to_num(&a, 256);
>        C_return(*c_ret);
> }
>
> to be called like this
>
> ( (foreign-lambda scheme-object "g2d_glyphs_baseline" g2d-glyph-vector) 
> glyphs)
>
> It compiles and I get weird results at runtime, so obviously am not
> building the cons cell correctly.
>

The C_alloc is a macro that actually does alloca(), so you are allocating
stack data that is not live after returning from the function. You will
either have to use `foreign-primitive`(to create a CPS function), or
pass a pre-allocated pair (which I recommend):

C_word g2d_glyphs_baseline(struct G2dGlyphs* glyphs, C_word p)
{
  C_set_block_item(p, 0, C_fix(10));
  C_set_block_item(p, 1, C_fix(256));
  return p;
}

(completely untested)


cheers,
felix




reply via email to

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