There are two reasons, each of which will be sufficient for yourself.
- The size of
INT32 is INT32 be 4 bytes. The size of INT24 also 4 bytes, because it contains the INT32 bit INT32 . Since myStruct contains 3 members of size 4, its size must be at least 12. - Presumably, the alignment requirement of
INT32 is 4. Thus, even if the size of INT24 is 3, the size of myStruct should still be 12, since it should have at least the alignment requirement of INT32 and therefore the size of myStruct should be complemented to the nearest multiple of 4.
anyway or workaround?
This is implementation specific, but the following combination may work for some compilers / cpu combinations. Refer to your compiler’s manual for the syntax of a similar function and the manual for your target processor to see if it supports uneven memory access. Also understand that non-smooth memory access has a performance limitation.
#pragma pack(push, 1) struct INT24 { INT32 data : 24; }; #pragma pack(pop) #pragma pack(push, 1) struct myStruct { INT32 a; INT32 b; INT24 c; }; #pragma pack(pop)
Bitfield packing may not work the same in all compilers. Be sure to check how you behave.
I think the standard way would be to store char arrays of sizes 3 and 4, and whenever you need to read or write a single integer, you need a std::memcpy value. This would be a bit cumbersome to implement and perhaps also slower than hacking the #pragma package.
user2079303
source share