Re: Packing for __nogc classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 02/22/04


Date: Sun, 22 Feb 2004 13:39:02 -0600

Edward Diener wrote:

>The reason for this post has to do with a potential common problem pre-.NET
>having to do with packing of classes/structures. If the programmer/creator
>of a struct/class does not specifically set a packing size, via the #pragma
>pack directive, when the compiler saw the header file it would apply
>whatever the packing was in the IDE ( or command-line ). This could lead to
>ABI problems if a library was built with a particular packing for a
>class/structure and the end-user of that library decided to change the
>global packing in the IDE ( or command-line ) to some other value. The
>solution to this problem was for the programmer/creator to set the packing
>for the class.structure in the header file for the class/structure via
>"#pragma pack". Then the compiler would honor this directive by overriding
>the end-user's global packing for that particular class/struct and all would
>be well with no ABI problem occurring. Every 3rd party implementation which
>distributed C++ header files/library would of course use the "#pragma pack"
>method to assure that no ABI problem would occur.

I think that's backwards. The real solution is to never use /Zp but instead
use #pragma pack on structs only as necessary. If you expect everybody to
use #pragma pack in order to write /Zp-neutral headers, you've effectively
made #pragma pack part of the language, which is, to put it mildly, not a
good thing. I'd personally like to see /Zp abolished, and I'd like #pragma
pack to generate warnings when its effects penetrate #includes, both coming
or going. An exception to this latter rule could be made for the
<pshpackN.h> and <poppack.h> Windows headers. BTW, the Windows headers are
not all /Zp-neutral.

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: padding mechanism in structures
    ... #pragma pack([n]) ... Specifies packing alignment for structure and union members. ... level by the pack pragma. ...
    (comp.lang.c)
  • Re: question about memcpy()
    ... | has pack() setting. ... If default packing is 8, there will be two bytes slack... ... such that it's usually not necessry to compile the entire ... #pragma ...
    (microsoft.public.vc.mfc)
  • Alignment issues with Sun C Compiler
    ... I have some serious problems with the pack() pragma with sun cc: ... I'd like to have the members of a few structures packed. ... whole structure at least on the natural alignment of the first member ...
    (comp.unix.solaris)
  • Re: Alignment issues with Sun C Compiler
    ... > I have some serious problems with the pack() pragma with sun cc: ... > I'd like to have the members of a few structures packed. ... > whole structure at least on the natural alignment of the first member ...
    (comp.unix.solaris)
  • Re: pragma Pack vs. Convention C, portability issue?
    ... -- Can we portably rely on pragma Pack taking precedence ... pragma Convention; ... -- This could be either 8 bits if the compiler lets pragma Pack ...
    (comp.lang.ada)