Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Windows (MSDev not mingw) #pragma pack(push, 1) doesn't work
Forum Updated to NodeBB v4.3 + New Features

Qt Windows (MSDev not mingw) #pragma pack(push, 1) doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davecotter
    wrote on 6 Mar 2019, 02:19 last edited by
    #1

    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?

    J 1 Reply Last reply 6 Mar 2019, 05:45
    0
    • D Offline
      D Offline
      davecotter
      wrote on 6 Mar 2019, 05:05 last edited by
      #4

      the answer to this case is to use uint32_t as the types not uint8_t

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 6 Mar 2019, 02:57 last edited by Kent-Dorfman 3 Jun 2019, 02:58
        #2

        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.

        See This C++ reference

        I would write code to manually stuff the bits when necessary, by using bit shift operators.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          davecotter
          wrote on 6 Mar 2019, 03:18 last edited by
          #3

          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

          K 1 Reply Last reply 6 Mar 2019, 07:42
          0
          • D Offline
            D Offline
            davecotter
            wrote on 6 Mar 2019, 05:05 last edited by
            #4

            the answer to this case is to use uint32_t as the types not uint8_t

            1 Reply Last reply
            0
            • D davecotter
              6 Mar 2019, 02:19

              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?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Mar 2019, 05:45 last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • D davecotter
                6 Mar 2019, 03:18

                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

                K Offline
                K Offline
                Kent-Dorfman
                wrote on 6 Mar 2019, 07:42 last edited by
                #6
                This post is deleted!
                1 Reply Last reply
                0

                1/6

                6 Mar 2019, 02:19

                • Login

                • Login or register to search.
                1 out of 6
                • First post
                  1/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved