For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > Data aligning









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Data aligning
Ozon

2005-06-10, 8:59 am

Hi,
please, could anyone explain me, why size of the following structure is 40
Bytes?

#pragma pack(16)
struct test01
{
DWORD member01;
char member02[21];
char member04;
__int64 member05;
};

member01 - 4 B
member02 - 21B
member04 - 1B
member05 - 8B

How is data aligned?
Why is size 40 Bytes?

Thanks for help.
Sergei

2005-06-10, 4:03 pm

"Ozon" <Ozon@discussions.microsoft.com> сообщил/сообщила в новостях следующее:
news:04B2CB3A-9564-41FE-9644-DEDC693268A9@microsoft.com...
> Hi,
> please, could anyone explain me, why size of the following structure is 40
> Bytes?
>
> #pragma pack(16)
> struct test01
> {
> DWORD member01;
> char member02[21];
> char member04;
> __int64 member05;
> };
>
> member01 - 4 B
> member02 - 21B
> member04 - 1B
> member05 - 8B
>
> How is data aligned?
> Why is size 40 Bytes?


offsets
0 member01 (4 bytes)
4 member02 (21 bytes) 1 byte alignment
25 member04 (1 byte) 1 byte alignment
26 6 padding bytes
32 member05 (8 bytes) 8 byte alignment

Sergei

Ozon

2005-06-10, 4:03 pm



"Sergei" wrote:

> "Ozon" <Ozon@discussions.microsoft.com> сообщил/сообщила в новостях следующее:
> news:04B2CB3A-9564-41FE-9644-DEDC693268A9@microsoft.com...
>
> offsets
> 0 member01 (4 bytes)
> 4 member02 (21 bytes) 1 byte alignment
> 25 member04 (1 byte) 1 byte alignment
> 26 6 padding bytes
> 32 member05 (8 bytes) 8 byte alignment
>
> Sergei


OK, but WHY?
Why is there 6 padding bytes before last member.
Why the whole structure is 40 B length when pragma pack is set to 16?.

Ozon
Sergei

2005-06-10, 4:03 pm

"Ozon" <Ozon@discussions.microsoft.com> wrote:
>
>
> "Sergei" wrote:
>
>
> OK, but WHY?


MSDN:
The alignment of a member will be on a boundary that is either a multiple of n or a multiple of the size of the member, whichever is
smaller

Sergei

> Why is there 6 padding bytes before last member.
> Why the whole structure is 40 B length when pragma pack is set to 16?.
>
> Ozon


Christian Ehlscheid

2005-06-10, 4:03 pm

Hello,

a structure must always be a multiple of the largest alignment requirement
of it's members which in this case it
__int64 which must be aligned on 8 byte boundary ..

imagine you declare an array of your structure:
struct test01 someName[10];

each member in your struct must be correctly aligned in each element of the
array, which wouldn't be the case if the compiler wouldn't compute the
offsets as it does currently.

Regards
Christian


Carl Daniel [VC++ MVP]

2005-06-10, 4:03 pm

Ozon wrote:
> "Sergei" wrote:
>
>
> OK, but WHY?
> Why is there 6 padding bytes before last member.
> Why the whole structure is 40 B length when pragma pack is set to 16?.


If you want the size to be 34 bytes, you need to set #pragma pack to 1.
Setting it to 16 instructs the compiler to use the smaller of 16 or the
field's natural alignment when laying out the structure. Since no element
in this struct has a natural alignment of more than 16, pragma pack(16) has
no effect on the layout of this struct.

The text in MSDN that explains alignment is not really right. It says "The
alignment of a member will be on a boundary that is either a multiple of n
or a multiple of the size of the member, whichever is smaller.". This is
true, provided that members are all built-in types. If a member was another
struct, however, then it would not be placed on a multiple of the struct
size. Rather, it would be placed on a multiple of the struct's aligment,
which is equal to the alignment of the most-restrictive member of the struct
(so if the struct contains an __int64, the struct would be 8-byte aligned,
but if the struct contained only chars, the struct would be 1-byte aligned).

Ty compiling the following code with different settings for /Zp to see the
effect.

<code>
#include <stdio.h>

struct A
{
char x;
};

struct B
{
__int64 x;
};

struct C
{
char a;
A x;
};

struct D
{
char a;
B x;
};

int main()
{
printf("Sizeof(C)=%d\n",sizeof(C));
printf("Sizeof(D)=%d\n",sizeof(D));
}
</code>

-cd


Scott McPhillips [MVP]

2005-06-11, 3:59 am

Ozon wrote:
> OK, but WHY?
> Why is there 6 padding bytes before last member.
> Why the whole structure is 40 B length when pragma pack is set to 16?.
>
> Ozon


I think all the answers you got talked about how to control this aspect
of the compiler, without really addressing WHY?

The memory bus is 32 bits wide. The processor reads 32 memory bits at a
time. If an int is not aligned at a 32-bit multiple the processor has
to read memory twice and put the correct 32 bits together from the two
parts before it can do anything with your int. The Pentium does this
automatically, but of course it takes twice as long to read a
non-aligned int. So the fundamental answer to "why" is that code
executes a lot faster when the variables are aligned.

Some other processors can not do the reassembly trick at all and
absolutely require that all operands be aligned.

--
Scott McPhillips [VC++ MVP]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com