[cffi-devel] foreign-type-size of struct types

Wilco Greven greven at kde.org
Fri Aug 5 07:24:28 UTC 2005


Op donderdag 04 augustus 2005 20:37, schreef Luis Oliveira:
> On 4/ago/2005, at 13:33, Wilco Greven wrote:
> > I'm using cffi for trying to revive the Qt bindings. Doing so I found
> > out that
> > for the calculation of the size of struct types, the alignment of the
> > struct
> > itself is not taking into account. For the following struct for example
>
> What should the alignment of a struct type be? I think it depends on
> the ABI and I think right now they are being aligned as if they were
> void* pointers, that doesn't sound right.
>
> > (defcstruct smoke-class
> >   (class-name string)
> >   (parents index)
> >   (class-fn :pointer)
> >   (enum-fn :pointer)
> >   (flags :unsigned-short))
>
> Is index a struct type? If not, what is the relevance of struct
> alignment here?

The example I gave was rather bad. Let me give a better one.

typedef struct {
  int a;
  short b;
} TestStruct;

(defcstruct test-struct
  (a :int)
  (e :short))

The problem arose when I wanted to access elements in an array of structs. Say 
I have an array "TestStruct *tests". I expected to be able to access the 
individual elements of this array by

(inc-ptr tests (* (foreign-struct-size 'test-struct) array-index)

The problem is that (foreign-type-size 'test-struct) returns a size of 6, 
while sizeof(TestStruct) returns 8. Therefore the code above doesn't work 
correctly.

My guess was that this had something to do with alignment of the struct, but I 
have to admit that my knowledge about memory alignment is pretty much nil. 
zero.

-- 
Wilco



More information about the cffi-devel mailing list