Struct alignment is always 4

105 Views Asked by At

Why pragma pack does not work in here? sizeof() returning the size aligned to 4. I use compiler which comes with Microsoft Visual Studio 2015

#pragma pack(push,1)

struct Header {
    uint16_t zero = 0;
    uint32_t id{};
};

struct ImageBlock : Header {
    uint32_t currentValidLength{};
};

#pragma pack(pop)
constexpr int sz = sizeof(ImageBlock);  // returns 12 but should be 10

Tried to also set Struct member alignment (/Zp1) compiler option with no luck

1

There are 1 best solutions below

0
Valerii Chistyakov On

Looks like it's an IntelliSense bug. At runtime, the size is correct, but Visual Studio shows me an incorrect value of sz while editing. I've also tried static_assert on the size: VS shows that it will fail at compile time, but in fact, everything is ok.