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

From: Henk Boom (lunarc.lists at gmail.com)
Date: Mon Jan 26 21:59:59 EST 2009

2009/1/26 Eli Barzilay <eli at barzilay.org>:
> Ah, I missed that.  No, this is forbidden: the GC won't know about it,
> so it will move the block leaving you with most likely invalid
> pointer.  (And from the rest of your message it looks like that's what
> happened.)
> This is the reason why pointers have offsets, that's probably the way
> to solve your problem.

I see. So instead of using make-ctype to do the conversion, I want to
use a custom accessor for the field, which takes a pointer to the
struct and offsets it to point to the array instead. Am I right so

I have looked through the pointer functions documentation,
specifically at the pointer offset functions. I've come up with the

(define _dReal _float)
(define _dVector3 _cvector)
(define _dVector3-direct
  (make-cstruct-type (list _dReal _dReal _dReal _dReal)))

(define-cstruct _foo
  ((private-i _dVector3-direct)
   (private-j _dVector3-direct)))

(define (foo-i f) (make-cvector* f _dReal 4))
(define (foo-f f) (make-cvector* (ptr-add f ???) _dReal 4)

The problem is in the definition of foo-f. I can't see any way of
finding the field's offset, since there seems not to be a pointer
difference function. Am I on the right track? Where should I look

If this is the way to properly do this, then I am surprised that
offsets are not used already. Why do we not have the same problem when
holding struct pointers to the middle of other structs? (or is the
problem that make-cvector* does not preserve offsets?)


Posted on the users mailing list.