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 compiler 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 ninechar‘s in thestruct.The new C++11 standard has an
alignaskeyword, but this may not be implemented in VC++08. Check your compiler’s manual (see e.g.__declspec(align(#))).