Friday, March 23, 2012

64 bit migration tips part 2

Pointer and structure padding: Fun times!

Consider a simple structure:
struct SStruct
{
  int m_nCount;
  int * m_pElements;
  int m_nMask;
};

In 32 bit land,
sizeof(SStruct) == 12;

In 64 bits:
sizeof(SStruct) == 20;

That's because m_pElements is a pointer, which is 8 bytes aligned, so now you have to remember to pad it out if you write it out as binary, or alternatively rejiggle the struct:

struct SStruct
{
  int * m_pElements;
  int m_nCount;
  int m_nMask;
};


Now in 64 bits:
sizeof(SStruct) == 16;


How can you write it from a 32 bit tool if you share the structure? Stay tuned...

No comments: