Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Structure alignement problem

5 views
Skip to first unread message

Arsalan Ahmad

unread,
Mar 15, 2005, 8:56:21 AM3/15/05
to
Hi all,

In my mfc application, in compiler settings i have changed struct member
alignment to 1 byte and this is working in debug mode. But when i tried to
do this in release mode of my application then i got following warning in
compilation of almost all the cpp files in release mode:

D:\arsalan\MyApplication\StdAfx.cpp(5) : warning C4653: compiler option
'structure packing (/Zp)' inconsistent with precompiled header; current
command-line option ignored

Compiling...

And thus my application in release mode is not working because i have some
structures which must be 1 byte aligned for their members.

Could you please tell me how can I make this work in release mode.

Thanks,

Arsalan


Mark Randall

unread,
Mar 15, 2005, 9:51:53 AM3/15/05
to
Set compiler settings in release config to the same. Also, you can clean
your project.

- MR

"Arsalan Ahmad" <a...@qosmotec.com> wrote in message
news:uxtvGbWK...@TK2MSFTNGP12.phx.gbl...

Mihajlo Cvetanovic

unread,
Mar 15, 2005, 10:26:35 AM3/15/05
to
Arsalan Ahmad wrote:
> And thus my application in release mode is not working because i have some
> structures which must be 1 byte aligned for their members.
>
> Could you please tell me how can I make this work in release mode.

Use #pragma, like this:

MyClass.h
---------
#pragma pack(push, myclass, 1)
class MyClass
{
...
};
#pragma pack(pop, myclass)

This way in all compilation units (in all cpp files) MyClass (and only
MyClass) will be seen as 1 byte aligned. Alignment on compiler level
should be set on default.

Doug Harrison [MVP]

unread,
Mar 15, 2005, 10:31:00 AM3/15/05
to

Instead of using /Zp, you should use #pragma pack along with #pragma
push/pop to control the packing of specific structs. Here's why.

You should not fool around with the default packing. While the CRT headers
and most of the Windows headers are diligent about setting the packing they
expect and restoring the original packing, this is far from universal.
You're begging for trouble if you use /Zp on code you didn't write
yourself, such as libraries you may be using, and even some of the Windows
headers, e.g. <wincon.h>, which last I checked (Jan 2004), has a problem
with its KEY_EVENT_RECORD struct under non-default packing. You can easily
cause your code to observe different class layouts than the libraries
you're using, which will really mess you up. On a non-Intel CPU, straying
from the default would almost certainly result in a horrendously slow
application or outright crashing due to misaligned data access. That's true
for cases in which changing the default packing *works*, in the sense that
everyone agrees on class layouts; on Intel CPUs, you would merely observe
slightly slower data access.

--
Doug Harrison
Microsoft MVP - Visual C++

Jonathan Arnold

unread,
Mar 15, 2005, 10:43:55 AM3/15/05
to
Doug Harrison [MVP] wrote:
> On Tue, 15 Mar 2005 14:56:21 +0100, Arsalan Ahmad wrote:
>
>
>>Hi all,
>>
>>In my mfc application, in compiler settings i have changed struct member
>>alignment to 1 byte and this is working in debug mode. But when i tried to
>>do this in release mode of my application then i got following warning in
>>compilation of almost all the cpp files in release mode:
>>
>>D:\arsalan\MyApplication\StdAfx.cpp(5) : warning C4653: compiler option
>>'structure packing (/Zp)' inconsistent with precompiled header; current
>>command-line option ignored
>>
>>Compiling...
>>
>>And thus my application in release mode is not working because i have some
>>structures which must be 1 byte aligned for their members.
>>
>>Could you please tell me how can I make this work in release mode.
>
>
> Instead of using /Zp, you should use #pragma pack along with #pragma
> push/pop to control the packing of specific structs. Here's why.

Or you can use the slightly more portable include files that are in
the Windows include folder:

#include <PshPack1.h> // or 2, 4 or 8

....

#include <PopPack.h>

This way, if/when you go to another compiler, you can wrap its compiler-specific
#pragmas and the like in the include files and not change your code.

--
Jonathan Arnold
inSORS

0 new messages