Optimal struct layout template?

Sergey Gromov snake.scaly at gmail.com
Wed Dec 17 13:12:18 PST 2008


Tue, 16 Dec 2008 10:09:41 +0100, Don wrote:

> Example 2:
> struct Foo { double, int } // 12 byte size, wants 8-byte alignment.

Foo.sizeof is 16.

The thing is, array of Foo's must be properly aligned, and &foo[1] must
be cast(byte*) &foo[0] + Foo.sizeof.  This means that Foo.sizeof must be
itself 8-aligned.  The solution is { Foo, Foo, int, int }.

The same with your other example.  Sizeof cannot be less than alignment.
Therefore you can never pack real and short in 16 bytes.



More information about the Digitalmars-d mailing list