Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is #pragma pack violates ODR for classes?

Reply
Thread Tools

Is #pragma pack violates ODR for classes?

 
 
Fedor Rodikov
Guest
Posts: n/a
 
      08-31-2011
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!
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-31-2011
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
 
Reply With Quote
 
 
 
 
Fedor Rodikov
Guest
Posts: n/a
 
      08-31-2011
On Aug 31, 8:58*am, Ian Collins <(E-Mail Removed)> 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 (VC200 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.


 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      08-31-2011
On Aug 31, 6:41*am, Fedor Rodikov <(E-Mail Removed)> 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.
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      08-31-2011
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
 
Reply With Quote
 
Fedor Rodikov
Guest
Posts: n/a
 
      09-01-2011
On Aug 31, 8:33*pm, Andrey Tarasevich <(E-Mail Removed)>
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!

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-01-2011
On Aug 31, 5:58 am, Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: ODR: A simple question James Kanze C++ 1 10-01-2008 09:37 AM
The ODR thing again Steven T. Hatton C++ 11 12-04-2006 02:11 PM
javax.swing.ButtonGroup's add method violates implied contract Kent Paul Dolan Java 22 12-22-2005 08:01 PM
CDP and ODR Yaron e Cisco 4 11-16-2005 07:57 AM
I finally figured it out! namespaces and the ODR Steven T. Hatton C++ 1 08-15-2004 01:22 PM



Advertisments