Optimal struct layout template?

Denis Koroskin 2korden at gmail.com
Wed Dec 17 13:21:58 PST 2008


On Thu, 18 Dec 2008 00:12:18 +0300, Sergey Gromov <snake.scaly at gmail.com>  
wrote:

> 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.
>

It is 12 with align(1) (I see no problem with it).

> 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 }.
>

Stop right there. You can't access &foo[1] no matter what align or  
structure layout is used. Example:

align(default):
struct Data
{
     int i1;
     short s2;
     char c1;
     int 12;
}

What is &i1[1]?



More information about the Digitalmars-d mailing list