Jump to content

Result of sizeof() not as expected

Gronnie

#pragma pack(1)struct ExampleStruct0 {    union {        struct {            uint32_t xxx    : 23;            uint32_t xxx    : 13;            uint32_t xxx    :  3;            uint32_t xxx    :  3;            uint32_t xxx    :  2;            uint32_t xxx    :  1;            uint32_t xxx    :  2;            uint32_t xxx    :  1;        };        struct {            uint32_t xxxLow     : 32;            uint32_t xxxHigh    :  7;            uint32_t xxx           :  1;        };    };};
#pragma pack(1)struct ExampleStruct1 {    union {        struct {            uint32_t xxx    : 23;            uint32_t xxx    : 13;            uint32_t xxx    :  3;            uint32_t xxx    :  3;            uint32_t xxx    :  2;            uint32_t xxx    :  1;            uint32_t xxx    :  2;            uint32_t xxx    :  1;        };        struct {            uint64_t xxx    : 39;            uint64_t xxx    :  1;        };    };};

What would you expect the result of sizeof(ExampleStruct0) to be?

How about sizeof(ExampleStruct1)?

I am getting something different than expected.

SSD Firmware Engineer

 

| Dual Boot Linux Mint and W8.1 Pro x64 with rEFInd Boot Manager | Intel Core i7-4770k | Corsair H100i | ASRock Z87 Extreme4 | 32 GB (4x8gb) 1600MHz CL8 | EVGA GTX970 FTW+ | EVGA SuperNOVA 1000 P2 | 500GB Samsung 850 Evo |250GB Samsung 840 Evo | 3x1Tb HDD | 4 LG UH12NS30 BD Drives | LSI HBA | Corsair Carbide 500R Case | Das Keyboard 4 Ultimate | Logitech M510 Mouse | Corsair Vengeance 2100 Wireless Headset | 4 Monoprice Displays - 3x27"4k bottom, 27" 1440p top | Logitech Z-2300 Speakers |

Link to comment
Share on other sites

Link to post
Share on other sites

Well at work the result is 8, but I just tried it on my home computer and I get what I expected -- 6. The #pragma pack(1) must not be working correctly at work.

It's not actually being done like in the example, it's being set in some weird way with a macro and a file with tons of ifdefs so we can write code to compile with several different compilers.

I've never done much with preprocessors, compilers, etc. I guess now is as good a time as any to learn.

SSD Firmware Engineer

 

| Dual Boot Linux Mint and W8.1 Pro x64 with rEFInd Boot Manager | Intel Core i7-4770k | Corsair H100i | ASRock Z87 Extreme4 | 32 GB (4x8gb) 1600MHz CL8 | EVGA GTX970 FTW+ | EVGA SuperNOVA 1000 P2 | 500GB Samsung 850 Evo |250GB Samsung 840 Evo | 3x1Tb HDD | 4 LG UH12NS30 BD Drives | LSI HBA | Corsair Carbide 500R Case | Das Keyboard 4 Ultimate | Logitech M510 Mouse | Corsair Vengeance 2100 Wireless Headset | 4 Monoprice Displays - 3x27"4k bottom, 27" 1440p top | Logitech Z-2300 Speakers |

Link to comment
Share on other sites

Link to post
Share on other sites

might be worth to give a look at this stackoverflow question: http://stackoverflow.com/questions/3919194/what-is-vc-doing-when-packing-bitfields

it seems that the bits are correctly aligned, but the compiler still reserves enough space for the lowest multiple of sizeof(uint_32t) that will fit your structure declaration

 

#pragma pack has the right to behave differently on every machine and compiler that you use it on, so it shouldn't be too much of a surprise if you get different results on different compilers

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×