www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - align(n) outside struct useless?

reply Michael Coulombe <kirsybuu gmail.com> writes:
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
Feb 20
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 20
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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...
Feb 20
parent kinke <noone nowhere.com> writes:
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.
Feb 20