3

Can anybody please explain what's going on?

My MSVC 2008 project's structure member alignment setting is set to 16 bytes (/Zp16) alignment, however one of the following structures is being aligned by 16 bytes and another is aligned only by 8 bytes... WHY?!!!

struct HashData { void *pData; const char* pName; int crc; bool bModified; }; // sizeof (HashData) == 4 + 4 + 4 + 1 + padding = 16 bytes, ok class StringHash { HashData data[1024]; int mask; int size; }; // sizeof(StringHash) == 1024 * 16 + 4 + 4 + 0 = 16392 bytes, why not 16400 bytes? 

This may not look like a big deal, but it's a big problem for me, since I am forced to emulate the MSVC structures alignment in GCC and specifying the aligned(16) attribute makes the sizeof (StringHash) == 16400!

Please tell me, when and why MSVC overrides the /Zp16 setting, I absolutely can't fathom it...

2 Answers 2

4

I think you misunderstood the /Zp16 option.

MSDN says,

When you specify this option, each structure member after the first is stored on either the size of the member type or n-byte boundaries (where n is 1, 2, 4, 8, or 16), whichever is smaller.

Please read the "whichever is smaller". It doesn't say that the struct will be padded by 16. It rather defines the boundary of each member relative to each other, starting from the first member.

What you basically want is align (C++) attribute, which says

Use __declspec(align(#)) to precisely control the alignment of user-defined data

So try this:

_declspec(align(16)) struct StringHash { HashData data[1024]; int mask; int size; }; std::cout << sizeof(StringHash) << std::endl; 

It should print what you expect.

Or you can use #pragma pack(16).

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Also OP, if you want to do something similar to __attribute__((aligned(16))) in MSVC it's #pragma pack(push, 16) struct /* ... */ #pragma pack(pop).
1

Consider using the pack pragma directive:

// Set packing to 16 byte alignment #pragma pack(16) struct HashData { void *pData; const char* pName; int crc; bool bModified; }; class StringHash { HashData data[1024]; int mask; int size; }; // Restore default packing #pragma pack() 

See: pack and Working with Packing Structures

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.