Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What does #pragma pack(0) do

What does #pragma pack(0) do

Scheduled Pinned Locked Moved C / C++ / MFC
question
21 Posts 7 Posters 0 Views 1 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.
  • Y yu jian

    The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?

    L Offline
    L Offline
    Lakamraju Raghuram
    wrote on last edited by
    #3

    Here n has to be 1,2 4, 8 .... Now if don't specify any value or if the value is 0, then the members are packed to default packing size (which is 8 for many compilers). However few compilers will throw compilation error.

    L 1 Reply Last reply
    0
    • C Chris Losinger

      it resets the packing to the default size (actually, what it does depends on the compiler. some compilers don't support pack(0) at all, and some use it as a reset)

      image processing toolkits | batch image processing

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh!

      ============================== Nothing to say.

      C 1 Reply Last reply
      0
      • L Lakamraju Raghuram

        Here n has to be 1,2 4, 8 .... Now if don't specify any value or if the value is 0, then the members are packed to default packing size (which is 8 for many compilers). However few compilers will throw compilation error.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh! (Just checked my code. Its been a few months since I did any, I guess the old memory is fading....) :)

        ============================== Nothing to say.

        L 1 Reply Last reply
        0
        • Y yu jian

          The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          #pragme pack 0 takes out all pading between data members in a struct so they are contiguous in memory. Very useful indeed since pretty much every data stream has no padding since it wastes bandwidth so being able to cast a chunk of memory to some zero packed struct gives you immediate and easy access to those data members. Consider an ethernet framed IP packet containing UDP and DHCP data. You can build a struct to grab the IP address requested directly from the data. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh!

          ============================== Nothing to say.

          1 Reply Last reply
          0
          • L Lost User

            Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh! (Just checked my code. Its been a few months since I did any, I guess the old memory is fading....) :)

            ============================== Nothing to say.

            L Offline
            L Offline
            Lakamraju Raghuram
            wrote on last edited by
            #7

            #pragma pack(0)

            #include
            using namespace std;

            struct Test
            {
            char a;
            int i;
            };

            void main()
            {
            cout<

            I am using VS2008 SP1. The build is x86.
            Now guess the result of sizeof(Test) ??

            L Y 2 Replies Last reply
            0
            • L Lakamraju Raghuram

              #pragma pack(0)

              #include
              using namespace std;

              struct Test
              {
              char a;
              int i;
              };

              void main()
              {
              cout<

              I am using VS2008 SP1. The build is x86.
              Now guess the result of sizeof(Test) ??

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              5

              ============================== Nothing to say.

              L 1 Reply Last reply
              0
              • L Lost User

                Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh!

                ============================== Nothing to say.

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #9

                Erudite_Eric wrote:

                pragma pack 0 means there is no packing between data members, so they are contiguous in memory.

                no. that's pack(1) : align on single bytes. pack(n) specifies the structure alignment, not the number of bytes between structs.

                image processing toolkits | batch image processing

                L 1 Reply Last reply
                0
                • L Lost User

                  5

                  ============================== Nothing to say.

                  L Offline
                  L Offline
                  Lakamraju Raghuram
                  wrote on last edited by
                  #10

                  I am reading 8 on my console.

                  L 1 Reply Last reply
                  0
                  • Y yu jian

                    The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #11

                    Why don't you continue reading MSDN? #pragma pack documentation[^] states:

                    Valid values are 1, 2, 4, 8, and 16.

                    Hence 0 is 'not valid' (I wouldn't try to make assumptions on a value marked as such).

                    Veni, vidi, vici.

                    Y 1 Reply Last reply
                    0
                    • Y yu jian

                      The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      yu-jian wrote:

                      But if n is zero, what will do?

                      Add /WX to your compiler settings and recompile. You should pay more attention to compiler warnings. :) Best Wishes, -David Delaune

                      Y 1 Reply Last reply
                      0
                      • L Lakamraju Raghuram

                        #pragma pack(0)

                        #include
                        using namespace std;

                        struct Test
                        {
                        char a;
                        int i;
                        };

                        void main()
                        {
                        cout<

                        I am using VS2008 SP1. The build is x86.
                        Now guess the result of sizeof(Test) ??

                        Y Offline
                        Y Offline
                        yu jian
                        wrote on last edited by
                        #13

                        8 in vs2008 sp1

                        1 Reply Last reply
                        0
                        • C CPallini

                          Why don't you continue reading MSDN? #pragma pack documentation[^] states:

                          Valid values are 1, 2, 4, 8, and 16.

                          Hence 0 is 'not valid' (I wouldn't try to make assumptions on a value marked as such).

                          Veni, vidi, vici.

                          Y Offline
                          Y Offline
                          yu jian
                          wrote on last edited by
                          #14

                          Just after read MSDN, I found that the case n=0 is ignored. I do not know why.

                          enhzflepE 1 Reply Last reply
                          0
                          • Y yu jian

                            Just after read MSDN, I found that the case n=0 is ignored. I do not know why.

                            enhzflepE Offline
                            enhzflepE Offline
                            enhzflep
                            wrote on last edited by
                            #15

                            Simply because it's not explicitly handled. Without giving the directive a special meaning for n=0, it makes perfect sense - it keeps the use of the directive consistent.

                            1 Reply Last reply
                            0
                            • L Lost User

                              yu-jian wrote:

                              But if n is zero, what will do?

                              Add /WX to your compiler settings and recompile. You should pay more attention to compiler warnings. :) Best Wishes, -David Delaune

                              Y Offline
                              Y Offline
                              yu jian
                              wrote on last edited by
                              #16

                              There is a error that Visual Studio 2008 only supports 1, 2, 4, 8... After add /WX to compiter.

                              L 1 Reply Last reply
                              0
                              • Y yu jian

                                There is a error that Visual Studio 2008 only supports 1, 2, 4, 8... After add /WX to compiter.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #17

                                See the documentation[^], which clearly states that the only valid values for n are 1, 2, 4, 8 and 16. Thus using 0 is an invalid #pragma and will be ignored: the default packing (8) will be used.

                                Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

                                Y 1 Reply Last reply
                                0
                                • C Chris Losinger

                                  Erudite_Eric wrote:

                                  pragma pack 0 means there is no packing between data members, so they are contiguous in memory.

                                  no. that's pack(1) : align on single bytes. pack(n) specifies the structure alignment, not the number of bytes between structs.

                                  image processing toolkits | batch image processing

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #18

                                  Doh! Quite correct. :doh:

                                  ============================== Nothing to say.

                                  1 Reply Last reply
                                  0
                                  • L Lakamraju Raghuram

                                    I am reading 8 on my console.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #19

                                    Yeah, it is 8, DWORD alignment. I got it wrong... :(

                                    ============================== Nothing to say.

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      See the documentation[^], which clearly states that the only valid values for n are 1, 2, 4, 8 and 16. Thus using 0 is an invalid #pragma and will be ignored: the default packing (8) will be used.

                                      Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

                                      Y Offline
                                      Y Offline
                                      yu jian
                                      wrote on last edited by
                                      #20

                                      Thank you for everyone's replies.

                                      1 Reply Last reply
                                      0
                                      • Y yu jian

                                        The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?

                                        A Offline
                                        A Offline
                                        Aescleal
                                        wrote on last edited by
                                        #21

                                        According to the C++ standard it can do whatever it likes. It's a way of implementors switching on non-standard features of the compiler. I think Griff and the others have told you enough about what it does on VC++ though! Cheers, Ash PS: Except this is the one thread Griff hasn't posted in. Let's try "Chris and the others..." instead!

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups