www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Alignment

reply "Artem Rebrov" <ar_other mail.ru> writes:
In following example size of struct B2 is 16. Why It is not 13? Is this a  
bug?

align(1) struct A1
{   byte a;
     int  b;
}
align(1) struct A2
{	byte a;
     int  b;
     A1 c;
}
align(4) struct B1
{   byte a;
     int  b;
}
align(1) struct B2
{   byte a;
     int  b;
     B1 c;
}

void main()
{
	printf("%d %d ",A1.sizeof,A2.sizeof); //prints 5 10
	printf("%d %d ",B1.sizeof,B2.sizeof); //prints 8 16
}

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Oct 24 2005
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Artem Rebrov" <ar_other mail.ru> wrote in message 
news:op.sy506hcuncj208 localhost...
 In following example size of struct B2 is 16. Why It is not 13? Is this a 
 bug?

 align(1) struct A1
 {   byte a;
     int  b;
 }
 align(1) struct A2
 { byte a;
     int  b;
     A1 c;
 }
 align(4) struct B1
 {   byte a;
     int  b;
 }
 align(1) struct B2
 {   byte a;
     int  b;
     B1 c;
 }

 void main()
 {
 printf("%d %d ",A1.sizeof,A2.sizeof); //prints 5 10
 printf("%d %d ",B1.sizeof,B2.sizeof); //prints 8 16
 }

I might be wrong, but I think it's because B1 itself is aligned on 4-byte boundaries. Thus, the memory layout of B2 is: abbb bxxx cccc cccc Where x is unused padding, and each letter represents one byte.
Oct 24 2005
parent reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
Hi.
Check Sean's post: he's right, it's the padding between 'a' and 'b', not 
between 'b' and 'c' like you've mentioned:

axxx bbbb cccc cccc

L.

"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:djjn9f$1mnf$1 digitaldaemon.com...
 "Artem Rebrov" <ar_other mail.ru> wrote in message 
 news:op.sy506hcuncj208 localhost...
 In following example size of struct B2 is 16. Why It is not 13? Is this a 
 bug?

 align(1) struct A1
 {   byte a;
     int  b;
 }
 align(1) struct A2
 { byte a;
     int  b;
     A1 c;
 }
 align(4) struct B1
 {   byte a;
     int  b;
 }
 align(1) struct B2
 {   byte a;
     int  b;
     B1 c;
 }

 void main()
 {
 printf("%d %d ",A1.sizeof,A2.sizeof); //prints 5 10
 printf("%d %d ",B1.sizeof,B2.sizeof); //prints 8 16
 }

I might be wrong, but I think it's because B1 itself is aligned on 4-byte boundaries. Thus, the memory layout of B2 is: abbb bxxx cccc cccc Where x is unused padding, and each letter represents one byte.

Oct 24 2005
parent reply "Uwe Salomon" <post uwesalomon.de> writes:
####
align(4) struct B1
{   byte a;
     int  b;
}
align(1) struct B2
{   byte a;
     int  b;
     B1 c;
}

int main()
{
   printf("%d %d \n",B2.a.offsetof,B2.b.offsetof);
}
####

This prints "0 1". This suggests that Sean is wrong, and Artem is right.   
:)

Ciao
uwe
Oct 25 2005
next sibling parent reply Sean Kelly <sean f4.ca> writes:
In article <op.sy7vt4so6yjbe6 sandmann.maerchenwald.net>, Uwe Salomon says...
####
align(4) struct B1
{   byte a;
     int  b;
}
align(1) struct B2
{   byte a;
     int  b;
     B1 c;
}

int main()
{
   printf("%d %d \n",B2.a.offsetof,B2.b.offsetof);
}
####

This prints "0 1". This suggests that Sean is wrong, and Artem is right.   
:)

This may be. Though for what it's worth, I ran the test program with the struct I posted and its sizeof was 13. Sean
Oct 25 2005
parent "Uwe Salomon" <post uwesalomon.de> writes:
 This may be.  Though for what it's worth, I ran the test program with  
 the struct
 I posted and its sizeof was 13.

Yes, and that's perfectly right. struct B2 { int a; // sizeof 4 B1 b; // sizeof is 8 byte c; // sizeof is 1 } As b is already aligned to a 4-byte-boundary, there is no padding between a and b. Try this: struct B2 { byte a; B1 b; int c; } Now there are 3 bytes padding between a and b, because a.sizeof is 1 and b is aligned on a 4-byte-boundary. Together that makes 12 bytes, and the 4 from int c result in 16 bytes B2.sizeof. Ciao uwe
Oct 26 2005
prev sibling parent reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
I'm an idiot! I forgot the align(1), which was the whole point :-S

L.

"Uwe Salomon" <post uwesalomon.de> wrote in message 
news:op.sy7vt4so6yjbe6 sandmann.maerchenwald.net...
 ####
 align(4) struct B1
 {   byte a;
     int  b;
 }
 align(1) struct B2
 {   byte a;
     int  b;
     B1 c;
 }

 int main()
 {
   printf("%d %d \n",B2.a.offsetof,B2.b.offsetof);
 }
 ####

 This prints "0 1". This suggests that Sean is wrong, and Artem is right. 
 :)

 Ciao
 uwe 

Oct 25 2005
parent Sean Kelly <sean f4.ca> writes:
Oops.  Now I understand what you intended to do.  Sorry about that.


Sean

In article <djn80a$8hs$1 digitaldaemon.com>, Lionello Lunesu says...
I'm an idiot! I forgot the align(1), which was the whole point :-S

L.

"Uwe Salomon" <post uwesalomon.de> wrote in message 
news:op.sy7vt4so6yjbe6 sandmann.maerchenwald.net...
 ####
 align(4) struct B1
 {   byte a;
     int  b;
 }
 align(1) struct B2
 {   byte a;
     int  b;
     B1 c;
 }

 int main()
 {
   printf("%d %d \n",B2.a.offsetof,B2.b.offsetof);
 }
 ####

 This prints "0 1". This suggests that Sean is wrong, and Artem is right. 
 :)

 Ciao
 uwe 


Oct 26 2005
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
In article <op.sy506hcuncj208 localhost>, Artem Rebrov says...
In following example size of struct B2 is 16. Why It is not 13? Is this a  
bug?

It's not 13 because of the order you have the variables declared. If you change it to this: align(1) struct B2 { int b; B1 c; byte a; } Then it will have a size of 13. The problem was the padding between a and b. Sean
Oct 24 2005
parent "Artem Rebrov" <ar_other mail.ru> writes:
On Tue, 25 Oct 2005 02:46:11 +0400, Sean Kelly <sean f4.ca> wrote:

 In article <op.sy506hcuncj208 localhost>, Artem Rebrov says...
 In following example size of struct B2 is 16. Why It is not 13? Is this  
 a
 bug?

you change it to this: align(1) struct B2 { int b; B1 c; byte a; } Then it will have a size of 13. The problem was the padding between a and b.

Unfortunately I can't change order of struct members. I use functions from a library, in which almost all structs aligned on 4-byte boundaries. Actually length of members is equal to 4. Although there is small number of structs, that have smaller members and struct-members. When I write D module based on C header file I must keep in mind that each struct can be a member of other struct. And I must add align(1) attribute to it even if its size is multiple of 4. Or when I use a struct as a member type I must check alignment and, if necessary, correct declaration. I rewrite my example in C to determine the source of such effect. ============= #include <stdio.h> #pragma pack(4) struct B1 { char a; int b; }; #pragma pack(1) struct B2 { char a; int b; struct B1 c; }; void main() { printf("%d %d ",sizeof(struct B1),sizeof(struct B2)); //prints 8 16 } ============= With DMC I get the same result, but with Microsoft CL it prints "8 13" as I expect. Therefore it matches the behavior of the companion C compiler. Is this more natural to pack my struct B2 as "abbb bccc cccc c" rather than "axxx bbbb cccc cccc"? The existing behaviour looks like "align(1) B2" takes no effect. Are there any related standards for C with which DMC is complied? -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Oct 26 2005