[plt-scheme] Binding C-Structs Containing Fixed-Size Arrays with the FFI

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Jan 26 08:20:32 EST 2009

At Sun, 25 Jan 2009 22:38:35 -0500, Henk Boom wrote:
> I'm trying to bind parts of ODE[1] with the FFI, but I've ran into an
> issue I haven't had to face before, and I haven't been able to resolve
> by looking at the FFI documentation. Take a look at the type
> definitions:
> typedef float dReal;
> //...
> typedef dReal dVector3[4];
> //...
> typedef struct dJointFeedback {
>   dVector3 f1;		/* force applied to body 1 */
>   dVector3 t1;		/* torque applied to body 1 */
>   dVector3 f2;		/* force applied to body 2 */
>   dVector3 t2;		/* torque applied to body 2 */
> } dJointFeedback;
> Now, as I understand it, dJointFeedback structs will basically contain
> 16 floats. I'm not sure how I should be binding this, though. The
> closest thing I have found looking through the documentation is
> _cvector[2], but that seems to actually wrap arrays-as-pointers, which
> would only let me define structs containing 4 float pointers, which
> doesn't work out.
> How should I handle this?

I would define `dVector3' as a structure:

 (define _dReal _float)
 (define-cstruct _dVector3 ([v0 _dReal] [v1 _dReal]
                            [v2 _dReal] [v3 _dReal]))

Defining a cstruct type doesn't work well for large arrays, but I bet
it will work well in this case.


Posted on the users mailing list.