Thread overview
how to set struct alignof?
Mar 22, 2005
Andrew Fedoniouk
Mar 22, 2005
Regan Heath
Mar 22, 2005
Derek Parnell
Mar 22, 2005
Derek Parnell
Mar 22, 2005
Andrew Fedoniouk
March 22, 2005
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.


March 22, 2005
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
March 22, 2005
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
March 22, 2005
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
March 22, 2005
Thanks a lot, guys.