zoukankan      html  css  js  c++  java
  • C++ sizeof(struct) 的注意

    今天在测试将C++代码导出的NavMesh二进制文件用一套C#改写的代码导入时,发现导入的数据出现不一致的问题.

    分别在C++和C#AddTile的函数内设置断点,观察最后得到的tile有大部分的字段是导入一致的.但是有些字段则出现的一些偏移.

    于是猜测应该为C#导入某个结构时出现了位偏移差错,追踪并对比每个字段的导入结果和偏移.最终发现这个结构导入时,出现了偏移变量相差2个单位

    在C++处,导入该结构前偏移变量 = 276, 然后导入时: = 276 + sizeof(dtPolyDetail) = 288.

    在C#处,导入该结构前偏移变量 = 276, 导入时: = 276 + sizeof(uint) + sizeof(uint) + sizeof(byte) + sizeof(byte) = 286.

    struct dtPolyDetail
    {
    unsigned int vertBase;
    unsigned int triBase;
    unsigned char vertCount;
    unsigned char triCount;
    };

    我们知道 sizeof(uint) = 4; sizeof(uchar) = sizeof(byte) = 1 但是sizeof(dtPolyDetail) != sizeof(uint) * 2 + sizeof(uchar) * 2 

    原因就是VC自动填充的对齐用到空字节:

    参考

    http://www.cnblogs.com/lazycoding/archive/2011/03/22/sizeof-struct.html

  • 相关阅读:
    sqhhb
    12333
    12

    今日份
    12
    彻底理解 Cookie、Session、Token
    https原理
    12312
    uiower
  • 原文地址:https://www.cnblogs.com/wmalloc/p/6344795.html
Copyright © 2011-2022 走看看