Is #pragma pack violates ODR for classes?

F

Fedor Rodikov

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!
 
I

Ian Collins

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.
 
F

Fedor Rodikov

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

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.
 
G

Goran

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.
 
A

Andrey Tarasevich

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.
 
F

Fedor Rodikov

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.

Now the subject is clear for me, especially the language's viewpoint.
Thanks all participants!
 
J

James Kanze

On 08/31/11 04:41 PM, Fedor Rodikov wrote:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,589
Members
45,100
Latest member
MelodeeFaj
Top