Structure alignment in Visual C++
c++, struct, visual-c++
Solution
Your excerpt explains this, "whichever is smaller". On a 32-bit platform, an `int` is 4 bytes. 4 is smaller than 8. So it has a 4-byte alignment.
The `pack` pragma causes things to be packed, not unpacked. It won't pad unless it has a reason to.
Problem
Visual C++ offers both a compiler switch (`/Zp`) and the `pack` pragma to affect the aligment of struct members. However, I seem to have some misconception as to how they work. According to MSDN, for a given alignment value n, 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. Let's assume a pack value of 8 bytes (which is the default). Within a struct, I'd think that any member whose size is less than 8 bytes will be at an offset that is a multiple of its own size. Any member whose size is 8 bytes or more will be at an offset that is a multiple of 8 bytes. Now take the following program: ``` #include <tchar.h> #pragma pack(8) struct Foo { int i1; int i2; char c; }; struct Bar { char c; Foo foo; }; int _tmain(int argc, _TCHAR* argv[]) { int fooSize = sizeof(Foo); // yields 12 Bar bar; int fooOffset = ((int) &bar.foo) - ((int) &bar); // yields 4 return 0; } ``` The `Foo` structure is 12 bytes in size. So within `Bar`, I'd expect the `Foo` member to be at offset 8 (a multiple of 8) while actually it's at offset 4. Why is that? Also, `Foo` really only has 4+4+1 = 9 bytes of data. The compiler automatically adds padding bytes at the end. But again, given an alignment value of 8 bytes, shouldn't it pad to a multiple of 8 rather than 4? Any clarification appreciated!