Thread overview
align(n) outside struct useless?
Feb 20, 2017
Michael Coulombe
Feb 20, 2017
Ali Çehreli
Feb 20, 2017
Adam D. Ruppe
Feb 20, 2017
kinke
February 20, 2017
I can't figure out how to set the alignment of a struct using align(n) on the outside of the struct. Only align on the fields (default or annotated) seems to work. I get the same results back to at least DMD 2.065... Is this a bug or am I using it wrong?

align(32) struct A { ubyte padding; }
pragma(msg, A.alignof); => 1

align(32) struct B { align(32) ubyte padding; }
pragma(msg, B.alignof); => 32

struct C { align(32) ubyte padding; }
pragma(msg, C.alignof); => 32

align(32) struct D { int padding; }
pragma(msg, D.alignof); => 4
February 20, 2017
On 02/20/2017 12:45 PM, Michael Coulombe wrote:
> I can't figure out how to set the alignment of a struct using align(n)
> on the outside of the struct. Only align on the fields (default or
> annotated) seems to work. I get the same results back to at least DMD
> 2.065... Is this a bug or am I using it wrong?

However, it works correctly:

enum A = 32;

align(A)
struct S {
    char c;
}

static assert(S.sizeof == A);

Ali

February 20, 2017
On Monday, 20 February 2017 at 20:45:27 UTC, Michael Coulombe wrote:
> I can't figure out how to set the alignment of a struct using align(n) on the outside of the struct.

align(n) on the outside of a struct will pad the struct as a whole to meet that size requirement. So it changes the sizeof and only really aligns when you have an array of the structs. I don't  believe it does anything (except perhaps pad bytes again) for a stack-defined struct.

align on the inside will shuffle the padding around in between members or pushing it to the end.

You'll likely want BOTH align, inside and outside, if you want to get a packed array of packed structs...
February 20, 2017
On Monday, 20 February 2017 at 22:45:09 UTC, Adam D. Ruppe wrote:
> I don't
> believe it does anything (except perhaps pad bytes again) for a stack-defined struct.

LDC does respect explicit type-alignments when allocating instances on the stack. The instances are obviously padded to their full size.