From: Orin E. <ori...@gm...> - 2010-01-27 04:41:18
|
On Tue, Jan 26, 2010 at 8:35 PM, Michael Plante <mic...@gm...>wrote: > Orin Eman wrote: > >> See the pack pragma. Not a bad idea to force the alignment in this case > as the > >> docs were insistent on it. > > Does pack apply to things not in structs/classes/unions? I ran some simple > test code, and it only seemed to matter when I enclose the three data items > below in a struct. Un-struct version at the end, with output. > > > >> I've seen some header files leave the packing something other than the > natural > >> alignment resulting in strange alignment warnings from the compiler. > > Not surprising. > > > > > ====== > #include <stdio.h> > > typedef unsigned __int64 HANDLE; > > #pragma pack(1) > > //struct { > HANDLE x; > unsigned __int32 y; > HANDLE z; > //} w; > #define X /*w.*/x > #define Y /*w.*/y > #define Z /*w.*/z > > int main() { > printf("%p,%p,%p\n", &X, &Y, &Z); > > return 0; > } > ======= > > w/o struct, it prints 00409AA0,00409AA8,00409AB0 > > w/ struct, it prints 00409AA0,00409AA8,00409AAC > > Clearly, it's aligned without the struct, but unaligned with the struct. > It > doesn't hurt to pack(push,8), but it may also not matter, regardless of the > headers. Obviously this could be compiler-dependent, and this may not even > be a good test, so perhaps we could just dump it in a struct anyway and > pack > it, to be safe. What do you think? > Yes, along with the /Zp compiler flag, #pragma pack affects structures only. Everything else should have natural alignment so it's probably only an issue if the variable is in a structure. Orin. |