align(n) not working as expected

Stewart Gordon smjg_1998 at yahoo.com
Tue Dec 28 14:50:22 PST 2010


On 28/12/2010 07:41, Robert Jacques wrote:
<snip>
> As per the docs, align behaves in the manner of the companion C++
> compile. DMC only defines align(1) and align(4), so they're the only two
> that work.
<snip>

But what is meant to happen if an alignment the companion C compiler 
doesn't support is used?  I would think not just silently ignore it.

See also
http://www.digitalmars.com/d/archives/digitalmars/D/Spec_of_align_attribute_is_a_mess_88129.html

In fact, it's for a different reason that it doesn't work in this 
instance.  Consecutive byte members are always packed, regardless of the 
alignment setting.  Other members smaller than the alignment setting are 
packed in a similar way.  Try this at home:

struct Temp {
     ubyte x;
     align(16) cdouble y;
}
pragma(msg, Temp.y.offsetof);

and likewise for align values 1, 2, 4 and 8.

By my experiments, it seems the maximum alignment for a given type is:
- for primitive types except real and creal, the type's size
- for real and creal, 2
- for static arrays, the element type's maximum alignment
- for all reference types, 4 (presumably always the platform pointer size)
- for structs, the highest maximum alignment of any member

Of course, other compilers may do it differently.

Stewart.


More information about the Digitalmars-d mailing list