The project that I’ve been working on involves porting some old code. Right now we are using VS2010 but the project is setup to use the VS2008 compiler and tool chain but eventually we will probably move all the way to the VS2010 toolchain. The struct in question looks like this:
struct HuffmanDecodeNode
{
union
{
TCHAR cSymbol;
struct
{
WORD nOneIndex;
WORD nZeroIndex;
} cChildren;
} uNodeData;
BYTE bLeaf;
}
For reasons that I won’t go into, sizeof(HuffmanDecodeNode) needs to be 8. I’m assuming that on the older compilers this worked out correctly, but now I’m seeing that the size is 6 unless I throw in some padding bytes. #pragma pack(show) confirms that the data should be 4 byte aligned which I assume used to be sufficient, but it appears that the newer compiler only uses this for alignment and doesn’t insert any trailing padding at the end of the struct.
Is there any way that I can control the trailing padding without just adding more struct members?
You can put
__declspec( align(8) )in front of your struct declaration.http://msdn.microsoft.com/en-us/library/83ythb65%28v=vs.100%29.aspx
but…
WCHAR has size 2 bytes, same for WORD. They both need alignment to 2 bytes only.
BYTE has size 1 byte and no alignment requirement.
I don’t think you need the 4 byte alignment in your struct.
http://msdn.microsoft.com/en-us/library/aa383751%28v=vs.85%29.aspx
P.S. In GCC you can do the same with
__attribute__ ((aligned (8))