Today, when you are testing a Navmesh binary that exports C + + code with a set of C # rewrite code imports, you find that the imported data appears to be inconsistent.
Set breakpoints in the functions of C + + and c#addtile, and observe that the final tile has most of the fields are imported consistently. However, some of the fields appear with some offsets.
Then guess that there was a bit offset error when importing a struct for C #, tracking and comparing the import results and offsets for each field. Finally, when this structure was imported, there was a difference of 2 units between the offset variable
In C + +, before importing the structure, the offset variable = 276 is then imported: = 276 + sizeof (dtpolydetail) = 288.
In C #, before importing the structure, the offset variable = 276, when imported: = 276 + sizeof (UINT) + sizeof (UINT) + sizeof (byte) + sizeof (byte) = 286.
struct Dtpolydetail
{
unsigned int vertbase;
unsigned int tribase;
unsigned char vertcount;
unsigned char tricount;
};
We know that sizeof (UINT) = 4; sizeof (UCHAR) = sizeof (byte) = 1 but sizeof (dtpolydetail)! = sizeof (UINT) * 2 + sizeof (UCHAR) * 2
The reason is that VC auto-fill alignment uses empty bytes:
Reference
Http://www.cnblogs.com/lazycoding/archive/2011/03/22/sizeof-struct.html
attention of C + + sizeof (struct)