Confusion about data alignment
suppose a struct defined like this:
struct S{
char a[3];
char b[3];
char c[3];
};
then what will be the output of printf("%d", sizeof(S)) ? On My c开发者_Python百科ompiler of Vc++ 2008 expression, the output is 9. And I got confused... I suppose the result be 12, but it is not. Shouldn't the compiler align the structure to 4 or 8 ?
The value of the sizeof
-expression is implementation-dependent; the only thing guaranteed by the C++ standard is that it must be at least nine since you're storing nine char
's in the struct
.
The new C++11 standard has an alignas
keyword, but this may not be implemented in VC++08. Check your compiler's manual (see e.g. __declspec(align(#))
).
There's nothing in S
that would force any of its members to be aligned other than per-byte so the compiler doesn't need to add any padding at all.
First, the alignment is implementation dependent, so it will depend on the compiler.
Now, remember that for a statically allocated array, the size need not be stored (the standard does not require it is), therefore it is usual for the alignment of an array to be the alignment of its elements.
Here, char[3]
thus has an alignment of 1
, and they are perfectly packed.
There is a compiler switch, /Zp, that allows you to set the default struct member alignment. There are also some other methods for specifying alignment in the c language.
Check out this MSDN post for details:
http://msdn.microsoft.com/en-us/library/xh3e3fd0(v=vs.80).aspx
Maybe your compiler is using one of these settings?
The typical requirement that each member be aligned only requires that the structure itself be aligned to the largest member type. Since all member types are char
, the alignment is 1
, so there's no need for padding. (For arrays, the base type (all extents removed) is what counts.)
Think about making an array of your structure: You'll want all the members of all the elements of that array to be aligned. But in your case that's just one large array of chars, so there's no need for padding.
As an example, suppose that on your platform sizeof(short) == 2
and that alignment equals size, and consider struct X { char a; short b; char c; };
. Then there's one byte internal padding between a
and b
to align b
correctly, but also one byte terminal padding after c
so that the entire struct has a size that's a multiple of 2
, the largest member size. That way, when you have an array X arr[10]
, all the elements of arr
will be properly aligned individually.
The compiler is given fairly wide latitude about how its aligns data. As a practical matter, however, the alignment of a datum will not exceed its size. That is, char
s must be byte-aligned, while int
s and long
s are often four-byte aligned.
Additionally, struct
s are aligned to the strictest alignment requirement of their members.
So, in your example, the strictest internal alignment requirement is 1-byte aligned, so the struct is 1-byte aligned. This means that it requires no padding.
精彩评论