attention of C + + sizeof (struct)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.