[plt-scheme] ffi equivalent of array in struct

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Apr 23 23:03:28 EDT 2006

On Apr 23, Jon Rafkind wrote:
> Is there an equivalent type in ffi to the array member of a struct in C?
> 
> Something like
> struct foo{
>   int blah[ 10 ];
> };

No.  There should be, but that requires a major rewrite of the foreign
type system, which I'll get to (eventually).  (You can put slots in
manually though.)


> Im creating objects that I dont want the GC to see so I use (malloc
> 'raw) but I have to give the size of the entire object to malloc as
> opposed to this:
> 
> (let ((f (malloc 'raw _Foo))
>    (set-Foo-blah! f (malloc 'raw _int 10)))

Whether you use _Foo or _int with 10, the result is the same -- the
type and the number are just used for the allocation, and there is no
type information with the pointer you're receiving.  (Which is another
thing that should change with the rewrite I mention above.)


> Obviously this doenst work but its sort of annoying to precalculate
> the size of the struct in C and use that as the size argument to
> malloc in scheme. Im guessing the way to do this is some trickery
> with the _bytes type? [...]

You can use `make-cstruct-type' with a list of 10 _int values, that
will create a struct type of the desired size.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.