Qt Windows (MSDev not mingw) #pragma pack(push, 1) doesn't work
-
i have some disk structures i need to read in that require structs to be tightly packed.
when i pack them with this:#pragma pack(push, 1)
they still come out wrong.
example program:
typedef uint8_t UInt8; #pragma pack(push, 1) typedef struct { UInt8 year : 6; // [0..63] + 2000 UInt8 month : 4; // [1..12] UInt8 day : 5; // [1..31] UInt8 hour : 5; // [0..23] UInt8 minute : 6; // [0..59] UInt8 second : 6; // [0..59] } ShortDateTime; #pragma pack(pop) #define kStructSize (int)4 int main(int argc, char *argv[]) { int actualI = (int)sizeof(ShortDateTime); if (actualI == kStructSize) { printf("it worked!"); } else { printf("%s actual: %d, should be: %d\n", "ShortDateTime", actualI, kStructSize); } }
output:
ShortDateTime actual: 6, should be: 4
so... what's missing?
-
the answer to this case is to use uint32_t as the types not uint8_t
-
Two things come to mind.
pragmas are compiler specific. what works in the gnu compile may not work in the M$ compiler.
Also, the specification does not guarantee adjacent bits being stored in the same byte.
I would write code to manually stuff the bits when necessary, by using bit shift operators.
-
It works on Mac with Xcode, and on Mac with QT. It works on windows with visual studio, and I am using VS2017 as the back end compiler for QT on windows, so why would it not work? We have all the information we need to get a definitive answer, it definitely should be working.
What is the proper way to pack bit fields? There is a way I just want to know what it is
Thanks
-
the answer to this case is to use uint32_t as the types not uint8_t
-
This post is deleted!
-
This post is deleted!