www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to set struct alignof?

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
As stated in D documetation (http://www.digitalmars.com/d/struct.html):

a.. [structure] "alignment can be explicitly specified"

The question is : how?

What notation I should use to define alignof for let's say
struct A
{
   ushort B;
}

Thanks. 
Mar 21 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 21 Mar 2005 16:25:33 -0800, Andrew Fedoniouk  
<news terrainformatica.com> wrote:
 As stated in D documetation (http://www.digitalmars.com/d/struct.html):

 a.. [structure] "alignment can be explicitly specified"

 The question is : how?
See: http://www.digitalmars.com/d/attribute.html#align Regan
Mar 21 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 22 Mar 2005 12:33:48 +1200, Regan Heath wrote:

 On Mon, 21 Mar 2005 16:25:33 -0800, Andrew Fedoniouk  
 <news terrainformatica.com> wrote:
 As stated in D documetation (http://www.digitalmars.com/d/struct.html):

 a.. [structure] "alignment can be explicitly specified"

 The question is : how?
See: http://www.digitalmars.com/d/attribute.html#align Regan
The 'align(x)' attribute is used to tell DMD how to align structures and not the structure members. To align the members inside a struct you need to set the struct alignment to 1, and then provide explicit fillers for gaps in the RAM mapping. align(1) struct MyStruct { byte A; byte[3] filler1; byte B; byte filler2; short C; } -- Derek Melbourne, Australia 22/03/2005 11:38:37 AM
Mar 21 2005
parent Derek Parnell <derek psych.ward> writes:
On Tue, 22 Mar 2005 11:42:11 +1100, Derek Parnell wrote:

 On Tue, 22 Mar 2005 12:33:48 +1200, Regan Heath wrote:
 
 On Mon, 21 Mar 2005 16:25:33 -0800, Andrew Fedoniouk  
 <news terrainformatica.com> wrote:
 As stated in D documetation (http://www.digitalmars.com/d/struct.html):

 a.. [structure] "alignment can be explicitly specified"

 The question is : how?
See: http://www.digitalmars.com/d/attribute.html#align Regan
The 'align(x)' attribute is used to tell DMD how to align structures and not the structure members. To align the members inside a struct you need to set the struct alignment to 1, and then provide explicit fillers for gaps in the RAM mapping. align(1) struct MyStruct { byte A; byte[3] filler1; byte B; byte filler2; short C; }
Ummmm... should this compile ok? import std.stdio; align(1) struct MyStruct { byte A; byte[3] filler1; byte B; byte filler2; short C; } void main() { MyStruct s; writefln("%d %d %d", s.A.offsetof, s.B.offsetof, s.C.offsetof); } -- Derek Melbourne, Australia 22/03/2005 11:42:38 AM
Mar 21 2005
prev sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
Thanks a lot, guys.
Mar 21 2005