Is #pragma pack violates ODR for classes?

Discussion in 'C++' started by Fedor Rodikov, Aug 31, 2011.

  1. Hi Group!

    Recently I met a problem in a legacy C++ project. This project has
    more than one translation unit which include header with class
    template X.
    This #include directive enclosed by #paragma pack in each cpp and the
    value of this packing not the same. Does the X violate ODR or not?
    Could a different packing in different translation units leads to UB?

    For example:

    // x.h
    template< typename >
    struct X { ... };

    // a.cpp
    #pragma pack( push )
    #pragma pack( 1 )
    #include "x.h"
    #pragma pack( pop )
    ....

    // b.cpp
    #pragma pack( push )
    #pragma pack( 8 )
    #include "x.h"
    #pragma pack( pop )
    ....

    Thanks in advance!
    Fedor Rodikov, Aug 31, 2011
    #1
    1. Advertising

  2. Fedor Rodikov

    Ian Collins Guest

    On 08/31/11 04:41 PM, Fedor Rodikov wrote:
    > Hi Group!
    >
    > Recently I met a problem in a legacy C++ project. This project has
    > more than one translation unit which include header with class
    > template X.
    > This #include directive enclosed by #paragma pack in each cpp and the
    > value of this packing not the same. Does the X violate ODR or not?
    > Could a different packing in different translation units leads to UB?


    If the compiler takes any notice of the pragma, it certainly would.

    --
    Ian Collins
    Ian Collins, Aug 31, 2011
    #2
    1. Advertising

  3. On Aug 31, 8:58 am, Ian Collins <> wrote:
    > On 08/31/11 04:41 PM, Fedor Rodikov wrote:
    >
    > > Hi Group!

    >
    > > Recently I met a problem in a legacy C++ project. This project has
    > > more than one translation unit which include header with class
    > > template X.
    > > This #include directive enclosed by #paragma pack in each cpp and the
    > > value of this packing not the same. Does the X violate ODR or not?
    > > Could a different packing in different translation units leads to UB?

    >
    > If the compiler takes any notice of the pragma, it certainly would.
    >
    > --
    > Ian Collins


    Thanks Ian for your comment!

    But my compiler (VC2008) doesn't take any notice.
    I can't rely only on absence of notices from my compiler and
    would to know exactly is this code correct or not.
    Fedor Rodikov, Aug 31, 2011
    #3
  4. Fedor Rodikov

    Goran Guest

    On Aug 31, 6:41 am, Fedor Rodikov <> wrote:
    > Hi Group!
    >
    > Recently I met a problem in a legacy C++ project. This project has
    > more than one translation unit which include header with class
    > template X.
    > This #include directive enclosed by #paragma pack in each cpp and the
    > value of this packing not the same. Does the X violate ODR or not?
    > Could a different packing in different translation units leads to UB?
    >
    > For example:
    >
    > // x.h
    > template< typename >
    > struct X { ... };
    >
    > // a.cpp
    > #pragma pack( push )
    > #pragma pack( 1 )
    > #include "x.h"
    > #pragma pack( pop )
    > ...
    >
    > // b.cpp
    > #pragma pack( push )
    > #pragma pack( 8 )
    > #include "x.h"
    > #pragma pack( pop )
    > ...
    >
    > Thanks in advance!


    Short:
    Different alignments will lead to crashes.


    Long:

    AFAIK, ODR is something language knows about and should enforce
    through a a compiler. I think that #pragma isn't part of the language.
    If so, I see no reason for the compiler to enforce language rules
    based on something language knows nothing about.

    Compilers might, however, warn about different alignment settings. I
    guess that should go under "quality of implementation".

    Goran.
    Goran, Aug 31, 2011
    #4
  5. On 8/30/2011 9:41 PM, Fedor Rodikov wrote:
    > This #include directive enclosed by #paragma pack in each cpp and the
    > value of this packing not the same. Does the X violate ODR or not?
    > Could a different packing in different translation units leads to UB?


    Well, '#pragma pack' is not really a part of the language specification
    (at least yet), so there's no formal answer to your question. But if you
    understand the rationale behind various ODR requirements, you have to
    conclude that using different packing for the same class in different
    translation units does indeed violate the intended restrictions of ODR.
    So, with above remarks, the behavior is de-facto undefined.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Aug 31, 2011
    #5
  6. On Aug 31, 8:33 pm, Andrey Tarasevich <>
    wrote:
    > On 8/30/2011 9:41 PM, Fedor Rodikov wrote:
    >
    > > This #include directive enclosed by #paragma pack in each cpp and the
    > > value of this packing not the same. Does the X violate ODR or not?
    > > Could a different packing in different translation units leads to UB?

    >
    > Well, '#pragma pack' is not really a part of the language specification
    > (at least yet), so there's no formal answer to your question. But if you
    > understand the rationale behind various ODR requirements, you have to
    > conclude that using different packing for the same class in different
    > translation units does indeed violate the intended restrictions of ODR.
    > So, with above remarks, the behavior is de-facto undefined.
    >
    > --
    > Best regards,
    > Andrey Tarasevich


    Now the subject is clear for me, especially the language's viewpoint.
    Thanks all participants!
    Fedor Rodikov, Sep 1, 2011
    #6
  7. Fedor Rodikov

    James Kanze Guest

    On Aug 31, 5:58 am, Ian Collins <> wrote:
    > On 08/31/11 04:41 PM, Fedor Rodikov wrote:


    > > Recently I met a problem in a legacy C++ project. This project has
    > > more than one translation unit which include header with class
    > > template X.
    > > This #include directive enclosed by #paragma pack in each cpp and the
    > > value of this packing not the same. Does the X violate ODR or not?
    > > Could a different packing in different translation units leads to UB?


    > If the compiler takes any notice of the pragma, it certainly would.


    Or not. The presence of #pragma pack makes the code
    implementation defined. If the implementation specifies that
    using #pragma pack with different values is well defined, then
    it's well defined. If the implementation specifies that it
    results in undefined behavior, then it is undefined behavior.
    And if the implementation doesn't specify when using the #pragma
    is defined, and when it isn't, then it's probably time to look
    for a different compiler. (Ideally, at least. Practically, I
    don't know of any compiler that fully specifies it's
    "implementation defined" behavior.)

    --
    James Kanze
    James Kanze, Sep 2, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    how does pragma pack work?

    JustSomeGuy, Dec 9, 2003, in forum: C++
    Replies:
    1
    Views:
    3,662
    Pete Becker
    Dec 9, 2003
  2. Steven T. Hatton
    Replies:
    1
    Views:
    307
    David Rubin
    Aug 15, 2004
  3. Jimmy

    Why #pragma pack not take effect?

    Jimmy, Jul 3, 2007, in forum: C Programming
    Replies:
    5
    Views:
    759
    Kenny McCormack
    Jul 4, 2007
  4. Boltar

    Question about using #pragma pack

    Boltar, Mar 20, 2008, in forum: C Programming
    Replies:
    7
    Views:
    386
    Keith Thompson
    Mar 20, 2008
  5. Steven Woody
    Replies:
    9
    Views:
    580
    Steven Woody
    Jul 13, 2008
Loading...

Share This Page