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. COM
  4. #pragma pack

#pragma pack

Scheduled Pinned Locked Moved COM
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.
  • A Offline
    A Offline
    anu_88
    wrote on last edited by
    #1

    hi , what is the meaning of #pragma pack ? From MSDN I came to know that it specifies the packing alignment for structure and union members . But what is meant by packing alignment ? Can you please tell me. Thanks in advance

    S 1 Reply Last reply
    0
    • A anu_88

      hi , what is the meaning of #pragma pack ? From MSDN I came to know that it specifies the packing alignment for structure and union members . But what is meant by packing alignment ? Can you please tell me. Thanks in advance

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

      If you declare a struct with char dummy; short x; long y; members, the "optimal" size would be 1 + 2 + 4 (assume x86 for the moment). However, this would be with alignment of 1. This says what byte boundary you use for packing. With an alignment of 2, each member begins on the next available 2 byte boundary, so the single char would be followed by an unnamed pad byte, the short would already be aligned correctly, as would the long. Similarly with 4 byte, the char is followed by 3 unnamed pad bytes, the short by another two, and the long would be OK as is. I can almost hear you asking "Why would I want to waste memory like that?" [fx: pause for reader to actually ask...] It's all to do with the way the processor accesses memory, and for some CPUs, like the SPARC, trying to access a packed (1) long would cause an exception. The important thing is to consistently use the same alignment for all in memory versions of a structure. For instance, if you build a DLL with a function that takes a structure, the declaration of that structure should really include #pragma pack(push) #pragma pack(n) struct { .... }; #pragma pack(pop) so that irrespective of the compile time options elsewhere, it has the packing n (where n is a power of 2). Without this, you might build the DLL with pack(2), for instance, and a client might be built with pack(1). This would misalign the data, and might even result in an access violation when you call the DLL function. Does that help? Steve S Developer for hire

      A 1 Reply Last reply
      0
      • S Steve S

        If you declare a struct with char dummy; short x; long y; members, the "optimal" size would be 1 + 2 + 4 (assume x86 for the moment). However, this would be with alignment of 1. This says what byte boundary you use for packing. With an alignment of 2, each member begins on the next available 2 byte boundary, so the single char would be followed by an unnamed pad byte, the short would already be aligned correctly, as would the long. Similarly with 4 byte, the char is followed by 3 unnamed pad bytes, the short by another two, and the long would be OK as is. I can almost hear you asking "Why would I want to waste memory like that?" [fx: pause for reader to actually ask...] It's all to do with the way the processor accesses memory, and for some CPUs, like the SPARC, trying to access a packed (1) long would cause an exception. The important thing is to consistently use the same alignment for all in memory versions of a structure. For instance, if you build a DLL with a function that takes a structure, the declaration of that structure should really include #pragma pack(push) #pragma pack(n) struct { .... }; #pragma pack(pop) so that irrespective of the compile time options elsewhere, it has the packing n (where n is a power of 2). Without this, you might build the DLL with pack(2), for instance, and a client might be built with pack(1). This would misalign the data, and might even result in an access violation when you call the DLL function. Does that help? Steve S Developer for hire

        A Offline
        A Offline
        anu_88
        wrote on last edited by
        #3

        Thank you very much for your reply

        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