Search code examples
c++pragma

Class Packing and Alignment


What is the alignment for calling #pragma pack() with no argument in GCC compiler?

Also, I often see that when we use placement new in creating object, the class is often being packed. What is the reason for that? Is it necessary to do so?


Solution

  • for the first part, you could have recklessly used google: http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html

    secondly there may be times when you need to control the binary format of your class. you may need to manage the amount of memory and the real binary offsets of data structures:

    struct
    {
      char c;
      int i;
    };
    

    may use 8 bytes of memory or 5 bytes of memory, according to machine type and pragma pack used.