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 is #pragma pack()?

What is #pragma pack()?

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 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.
  • X Offline
    X Offline
    xSoptik
    wrote on last edited by
    #1

    Hello all. Can someone tell me in simple way what happen when I use preprocessor option #pragma pack ? I tryed it but I thing there is no effect. Can you tell me what exactly happen? Thank you a lot. // sample code #pragma pack(1) // turn byte alignment on ????? enum mENUM { // an enum }; struct p_mStruct { UINT m_Ui; // and so on }; #pragma pack() // turn byte alignment off ????? Jaja Paja

    S 1 Reply Last reply
    0
    • X xSoptik

      Hello all. Can someone tell me in simple way what happen when I use preprocessor option #pragma pack ? I tryed it but I thing there is no effect. Can you tell me what exactly happen? Thank you a lot. // sample code #pragma pack(1) // turn byte alignment on ????? enum mENUM { // an enum }; struct p_mStruct { UINT m_Ui; // and so on }; #pragma pack() // turn byte alignment off ????? Jaja Paja

      S Offline
      S Offline
      Stlan
      wrote on last edited by
      #2

      Perhaps a simple example is better than any explanation. First case: #pragma pack(1) struct MyStruct { BYTE a; UINT b; }; #pragma pack() sizeof(struct MyStruct) will return 5 bytes (1+4=5) Second case: #pragma pack(4) struct MyStruct { BYTE a; UINT b; }; #pragma pack() sizeof(struct MyStruct) will now return 8 bytes! (1+3+4=8) because the compiler aligns each member of MyStruct to a 4 bytes boundary. Concretly, the directive tells the compiler that the address of each member must be divisible by 4. To do that, the compiler insert the necessary blank bytes between variable members.

      X 1 Reply Last reply
      0
      • S Stlan

        Perhaps a simple example is better than any explanation. First case: #pragma pack(1) struct MyStruct { BYTE a; UINT b; }; #pragma pack() sizeof(struct MyStruct) will return 5 bytes (1+4=5) Second case: #pragma pack(4) struct MyStruct { BYTE a; UINT b; }; #pragma pack() sizeof(struct MyStruct) will now return 8 bytes! (1+3+4=8) because the compiler aligns each member of MyStruct to a 4 bytes boundary. Concretly, the directive tells the compiler that the address of each member must be divisible by 4. To do that, the compiler insert the necessary blank bytes between variable members.

        X Offline
        X Offline
        xSoptik
        wrote on last edited by
        #3

        Ok. I understand now. Thank you a lot. xSoptik

        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