Re: Do not trust ALIGN

Tech-Archive recommends: Fix windows errors by optimizing your registry



#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


.



Relevant Pages

  • Re: padding mechanism in structures
    ... compiler options are inherently compiler specific. ... > data-declaration level by the pack pragma. ... > structure member after the first is stored on the smaller member type or ... If you use #pragma pack without an argument, ...
    (comp.lang.c)
  • Re: Question about setjmp on Itanium HPUX.
    ... object of that type will be aligned properly, even if it's a member of ... I would expect the compiler to do whatever it needs to to make obj.d ... pragma, but the hardware requires stricter alignment, the compiler ... The behavior might cause translation to fail or cause the ...
    (comp.lang.c)
  • Re: pragma Pack vs. Convention C, portability issue?
    ... -- Can we portably rely on pragma Pack taking precedence ... pragma Convention; ... -- This could be either 8 bits if the compiler lets pragma Pack ...
    (comp.lang.ada)
  • Re: Why does #pragma pack fail to function?
    ... understand #pragma pack. ... or similar and not a #pragma. ... Those can be combined by the compiler into a single object of the ... I'll follow your advice from now on. ...
    (microsoft.public.vc.language)
  • Re: sizeof(class x)
    ... Just to complete, in Vc, you can fix/adjust alignment of class members using ... pragma compiler directive, for instance: ... #pragma pack ...
    (microsoft.public.vc.language)