Re: Do not trust ALIGN
- From: "Dean Ramsier" <ramsiernospam@xxxxxxxxxx>
- Date: Thu, 17 Aug 2006 15:01:57 -0400
#pragma pack(1)
struct foo {
UINT8 reg1;
UNALIGNED UINT32 reg2;
UINT8 reg4;
UNALIGNED UINT32 reg5;
UINT16 reg6; // NOTE: this one falls on a naturally aligned offset
soUNALIGNED is not required.
UINT32 reg7;// same here...
};
#pragma pack
Although I consider it a BUG in the compiler if those UNALIGNED atributes
are actually required as the compiler knows the offsets are not correctly
aligned via the presence of the #pragma pack so it can deduce the need for
unaligned instruction sequences for accessing those members. (Although I
still recommend using UNALIGNED here for programmer clarity.) The compiler
can't figure out the rare case when a pointer to a structure just happens
to be at an odd value such that some of the otherwise unaligned members
actually end up aligned so it will do things as unaligned no matter what.
#pragma pack(1) should do the exact same thing as explicitly listing the
alignment of each member to it's native size.
#pragma pack(1) doesn't do the same thing as aligning each member to it's
native size, it tells the compiler to override natural alignment and pack
each item on the next 1 byte boundary. The compiler will automatically
align each structure member to its native size unless that behavior is
overridden by #pragma pack(n).
See a posting by Russ K on this topic...
http://blogs.msdn.com/ce_base/archive/2006/04/14/576545.aspx
--
Dean Ramsier - eMVP
BSQUARE Corporation
.
- Follow-Ups:
- Re: Do not trust ALIGN
- From: Steve Maillet \(eMVP\)
- Re: Do not trust ALIGN
- References:
- Do not trust ALIGN
- From: Ronald Sloot
- Re: Do not trust ALIGN
- From: Andrew at Plextek
- Re: Do not trust ALIGN
- From: Dean Ramsier
- Re: Do not trust ALIGN
- From: Steve Maillet \(eMVP\)
- Re: Do not trust ALIGN
- From: Andrew at Plextek
- Re: Do not trust ALIGN
- From: Steve Maillet \(eMVP\)
- Do not trust ALIGN
- Prev by Date: Re: Do not trust ALIGN
- Next by Date: Re: Do not trust ALIGN
- Previous by thread: Re: Do not trust ALIGN
- Next by thread: Re: Do not trust ALIGN
- Index(es):
Relevant Pages
|