www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I still don't understand the align attribute

reply Trass3r <un known.com> writes:
http://digitalmars.com/d/2.0/attribute.html#align

"align by itself sets it to the default, which matches the default member  
alignment of the companion C compiler."
OK!

"align(Integer): Integer specifies the alignment which matches the  
behavior of the companion C compiler when non-default alignments are used"
I don't really understand that sentence.

"struct S
{   align(4) byte a;	// placed at offset 0
     align(4) byte b;	// placed at offset 1
}"

So those align(4)s are completely useless since the default alignment is  
chosen anyways?



"align (1) struct S
{   byte a;	// placed at offset 0
     byte[3] filler1;
     byte b;	// placed at offset 4
     byte[3] filler2;
}"

This is just as I would expect but this leaves no chance to specify  
alignment for individual members, does it?
Jun 02 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Trass3r:
 So those align(4)s are completely useless since the default alignment is  
 chosen anyways?

On 64 bit CPUs/systems the alignment can be 8, so if you ask for 4 you get 4 instead of 8.
 This is just as I would expect but this leaves no chance to specify  
 alignment for individual members, does it?

You can put the alignment to single members. I suggest you to print the .alignof attribute of all your members in your experiments, so you can answer to your own questions in a quick way. Bye, bearophile
Jun 03 2010
parent bearophile <bearophileHUGS lycos.com> writes:
 I suggest you to print the .alignof attribute of all your members in your
experiments, so you can answer to your own questions in a quick way.

I meant the .offsetof attribute, sorry.
Jun 03 2010
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Trass3r wrote:
 http://digitalmars.com/d/2.0/attribute.html#align
 
 "align by itself sets it to the default, which matches the default 
 member alignment of the companion C compiler."
 OK!
 
 "align(Integer): Integer specifies the alignment which matches the 
 behavior of the companion C compiler when non-default alignments are used"
 I don't really understand that sentence.

Basically, it means that the member is aligned on n-byte boundaries in whatever way the companion C compiler does if instructed to. However, there's no statement I can find of what's supposed to happen if the companion C compiler doesn't support alignment control at the level where it is used. http://d.puremagic.com/issues/show_bug.cgi?id=3183 (marked as fixed, but it seems it isn't really) http://www.digitalmars.com/d/archives/digitalmars/D/Spec_of_align_attribute_is_a_mess_88129.html Stewart.
Jun 17 2010