Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9536 > unrolled thread

Is #pragma pack violates ODR for classes?

Started byFedor Rodikov <rodikov.fedor@gmail.com>
First post2011-08-30 21:41 -0700
Last post2011-09-01 02:52 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  Is #pragma pack violates ODR for classes? Fedor Rodikov <rodikov.fedor@gmail.com> - 2011-08-30 21:41 -0700
    Re: Is #pragma pack violates ODR for classes? Ian Collins <ian-news@hotmail.com> - 2011-08-31 16:58 +1200
      Re: Is #pragma pack violates ODR for classes? Fedor Rodikov <rodikov.fedor@gmail.com> - 2011-08-31 00:05 -0700
      Re: Is #pragma pack violates ODR for classes? James Kanze <james.kanze@gmail.com> - 2011-09-01 16:00 -0700
    Re: Is #pragma pack violates ODR for classes? Goran <goran.pusic@gmail.com> - 2011-08-31 00:23 -0700
    Re: Is #pragma pack violates ODR for classes? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-08-31 09:33 -0700
      Re: Is #pragma pack violates ODR for classes? Fedor Rodikov <rodikov.fedor@gmail.com> - 2011-09-01 02:52 -0700

#9536 — Is #pragma pack violates ODR for classes?

FromFedor Rodikov <rodikov.fedor@gmail.com>
Date2011-08-30 21:41 -0700
SubjectIs #pragma pack violates ODR for classes?
Message-ID<307c82de-3f68-45dd-911a-f9b748a9d710@n12g2000yqh.googlegroups.com>
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!

[toc] | [next] | [standalone]


#9537

FromIan Collins <ian-news@hotmail.com>
Date2011-08-31 16:58 +1200
Message-ID<9c5tbfF5dgU7@mid.individual.net>
In reply to#9536
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

[toc] | [prev] | [next] | [standalone]


#9540

FromFedor Rodikov <rodikov.fedor@gmail.com>
Date2011-08-31 00:05 -0700
Message-ID<e081e958-1cff-4ba4-9009-ede0d30714c2@t7g2000vbv.googlegroups.com>
In reply to#9537
On Aug 31, 8:58 am, Ian Collins <ian-n...@hotmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#9629

FromJames Kanze <james.kanze@gmail.com>
Date2011-09-01 16:00 -0700
Message-ID<a9e95bf0-16f8-4be9-ba3e-f0680adf9a6f@k9g2000vbd.googlegroups.com>
In reply to#9537
On Aug 31, 5:58 am, Ian Collins <ian-n...@hotmail.com> 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

[toc] | [prev] | [next] | [standalone]


#9541

FromGoran <goran.pusic@gmail.com>
Date2011-08-31 00:23 -0700
Message-ID<462f4c81-38c0-4097-af91-16a69d943469@gz5g2000vbb.googlegroups.com>
In reply to#9536
On Aug 31, 6:41 am, Fedor Rodikov <rodikov.fe...@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#9566

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2011-08-31 09:33 -0700
Message-ID<j3lnnl$k2d$1@dont-email.me>
In reply to#9536
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

[toc] | [prev] | [next] | [standalone]


#9597

FromFedor Rodikov <rodikov.fedor@gmail.com>
Date2011-09-01 02:52 -0700
Message-ID<f21aef4b-e928-422f-877b-3beaeb87f846@fd21g2000vbb.googlegroups.com>
In reply to#9566
On Aug 31, 8:33 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web