Structure packing problem with VC 6.0.... 
Author Message
 Structure packing problem with VC 6.0....

I have a project that I build for multiple platforms which, up until
now, I've been compiling with Watcom C++.  I recently decided to try
using MS Visual C++ to build the project.  (Better support by third
parties, etc.)  I've managed to convert it over nicely, but I've found
one thing about VC that annoys me that I can't seem to fix.

The problem is with structure packing.  With Watcom, I set the packing
to 1 byte alignment.  (Saves trouble with packing on various other
platforms.)  When I started with VC, I turned this on and started to
encounter problems with the Windows standard structures.  As an
example:

#include <windows.h>

...

int GetInput(char Wait, KeyEvent *K, MouseEvent *M) {
  ...
  INPUT_RECORD IR;

  ...
  ... {
    KEY_EVENT_RECORD KE = IR.Event.KeyEvent;

...

INPUT_RECORD is a 4 byte aligned structure (in WINCON.H), surrounded
by #pragmas which are meant to keep it at 4 byte alignment.  If I set
byte alignment to 1 byte (/Zp1), when VC compiles this source and does
the assignment of IR.Event.KeyEvent to KE, it treats it like a 1 byte
aligned structure.  If I take out the /Zp1, it treats it correctly.
It seems as if the /Zp1 is causing VC to ignore the #pragma packs
which surround the structure in WINCON.H.

I've managed to kludge around this problem by putting #pragma pack(1)
statements at the start of most of my headers, but I would really like
to avoid the kludge.  Is there some way to get VC to behave correctly,
i.e. apply 1 byte packing to user code and correct packing (whatever
that may be) to the Windows standard structures?



Fri, 28 Jun 2002 03:00:00 GMT  
 Structure packing problem with VC 6.0....

Quote:

>I have a project that I build for multiple platforms which, up until
>now, I've been compiling with Watcom C++.  I recently decided to try
>using MS Visual C++ to build the project.  (Better support by third
>parties, etc.)  I've managed to convert it over nicely, but I've found
>one thing about VC that annoys me that I can't seem to fix.

>The problem is with structure packing.  With Watcom, I set the packing
>to 1 byte alignment.  (Saves trouble with packing on various other
>platforms.)  When I started with VC, I turned this on and started to
>encounter problems with the Windows standard structures.  As an
>example:

>#include <windows.h>

>...

>int GetInput(char Wait, KeyEvent *K, MouseEvent *M) {
>  ...
>  INPUT_RECORD IR;

>  ...
>  ... {
>    KEY_EVENT_RECORD KE = IR.Event.KeyEvent;

>...

>INPUT_RECORD is a 4 byte aligned structure (in WINCON.H), surrounded
>by #pragmas which are meant to keep it at 4 byte alignment.  If I set
>byte alignment to 1 byte (/Zp1), when VC compiles this source and does
>the assignment of IR.Event.KeyEvent to KE, it treats it like a 1 byte
>aligned structure.  If I take out the /Zp1, it treats it correctly.
>It seems as if the /Zp1 is causing VC to ignore the #pragma packs
>which surround the structure in WINCON.H.

>I've managed to kludge around this problem by putting #pragma pack(1)
>statements at the start of most of my headers, but I would really like
>to avoid the kludge.  Is there some way to get VC to behave correctly,
>i.e. apply 1 byte packing to user code and correct packing (whatever
>that may be) to the Windows standard structures?

Are you sure the problem is where you think it is? For the program
below, I get the following results in VC6, SP3:

cl a.cpp
8, 8

cl -Zp1 a.cpp
8, 5

This looks fine to me. If the necessary pragmas are present in
wincon.h, I would expect them to work.

*****

#include <stdio.h>

#include <pshpack4.h>

struct A
{
   char x;
   int y;

Quote:
};

#include <poppack.h>

struct B
{
   char x;
   int y;

Quote:
};

void main()
{
   printf("%d, %d\n",(int) sizeof(A),(int) sizeof(B));

Quote:
}

--
Doug Harrison

Visual C++ MVP


Sat, 29 Jun 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. W2K Service Pack 1 - VC ++ 6.0 Debugger Problem

2. W2K Service Pack 1 - VC ++ 6.0 Debugger Problem

3. VC 6.0 service pack 3

4. VC 6.0 Service Pack 5

5. VC 6.0 Service Pack?

6. Problem to convert from VC 6.0 to VC 7.1 because of using filebuf::setmode()

7. Structure packing problem

8. VC 5.0 and VC 6.0 compatibility problem ?

9. vc++ 5.0 to vc++ 6.0 problem

10. Problem upgrading from VC 5.0 to VC 6.0

11. Structure packing problem in WIN32_FIND_DATA, FindFirstFile?

12. Is a Structure Contigous? / Structure Packing

 

 
Powered by phpBB® Forum Software