简体   繁体   English

MSVC:什么编译器开关会影响结构的大小?

[英]MSVC: what compiler switches affect the size of structs?

I have two DLLs compiled separately, one is compiled from Visual Studio 2008 and one is a mex file compiled from matlab. 我有两个单独编译的DLL,一个是从Visual Studio 2008编译的,另一个是从matlab编译的mex文件。
Both DLLs have a header file which they include. 这两个DLL都有一个包含的头文件。 when I take the sizeof() the struct in one DLL it returns 48, and in the other it returns 64. I've checked the /Zp switch and in both compilations it is set to /Zp8 . 当我在一个DLL中使用sizeof()结构时它返回48,而在另一个DLL中它返回64.我检查了/Zp开关,在两个编译中它都被设置为/Zp8
What other compiler switches may affect the size of a struct? 其他编译器切换可能会影响结构的大小?
The struct is a simple POCO with no inheritance and no virtual functions. 结构是一个简单的POCO,没有继承,也没有虚函数。


Edit 编辑

The struct looks like this: 结构看起来像这样:

class LIBSPEC SGeometry
{
public:

    std::vector<IGeometry> m_i;
    uint N;
    uint n_im, n_s;
};

In debug it sizeof() returns 56 in both cases, in release, in the mex compilation it's 48, from VS it's 64. 在调试中, sizeof()在两种情况下都会返回56,在发布中,在mex编译中它是48,来自VS它的64。
I can tell matlab the exact compiler options to use when compiling the mex so it's not it. 我可以告诉matlab在编译mex时使用的确切编译器选项,所以不是这样。


Edit 编辑

After checking with offsetof, it turns out that the difference is in the size of the std::vector . 在用offsetof检查之后,结果发现差异在于std::vector的大小。 in one dll it's 32 and in the other it's 48. 在一个dll中它是32,而在另一个dll中它是48。
Both dlls are x64. 两个dll都是x64。

Ok, so this is possibly the most obscure thing ever. 好的,所以这可能是有史以来最晦涩的事情。
It turns out that matlab add /D_SECURE_SCL=0 to the compilation which disables something called 'secure iterators' 事实证明,matlab将/D_SECURE_SCL=0添加到编译中,该编译禁用了一个名为'secure iterators'的东西
This in turn causes a difference of 16 bytes in the size of std::vector 这反过来导致std::vector大小相差16个字节

  • Check for differences in the include and library paths 检查包含和库路径的差异
  • Check for differences in defined preprocessor symbols 检查已定义的预处理器符号的差异
  • Check for any differences in the project settings (code generation, linkage etc.) 检查项目设置的任何差异(代码生成,链接等)
  • Do compile both versions with the same IDE on the same machine? 在同一台机器上使用相同的IDE编译两个版本?

Target platform (eg 32bit vs. 64 bit) Also /Zp can be overridden by #pragma statements. 目标平台(例如32位与64位)同样/ Zp可以被#pragma语句覆盖。 If you use pointers to members in the struct, the /vm? 如果使用指向结构中成员的指针,/ vm? option affects their size. 选项影响其大小。

Type definitions can be affected by different files #included. 类型定义可能受不同文件#included的影响。

I can't think of anything else (all rather unlikely). 我想不出别的什么(都不太可能)。

It would probably help if you post the struct declaration. 如果你发布结构声明,它可能会有所帮助。 You can also use OFFSETOF macro and sizeof() on the individual elements to explore the differences in detail. 您还可以在各个元素上使用OFFSETOF宏和sizeof()来详细探索差异。

#pragma pack might be used when one or the other library is built that might change the size of structs independent of whatever's on the command line for the compiler. 构建一个或另一个库时,可能会使用#pragma pack ,它可能会改变结构的大小,而与编译器命令行上的任何内容无关。

Another thing that can be responsible for size differences is if any typedefs used in the declaration of the struct might be defined to be one type or another using conditional compilation. 另一个可能导致大小差异的事情是,如果结构声明中使用的任何typedef可能使用条件编译定义为一种类型或另一种类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译器是否优化大小为0的结构? - Does the compiler optimise structs of size 0? MSVC 编译器中的 /Fx 选项是什么意思? - What is the meaning of the /Fx option in MSVC compiler? MSVC C++ 编译器在什么情况下有时会在 function 运算符 new[] 返回的指针之前直接写入数组大小? - Under what conditions does MSVC C++ Compiler sometimes write the array size directly before the pointer returned from function operator new[]? 将结构移植到 MSVC 2017 - Porting structs to MSVC 2017 什么是Visual Studio编译器的record-gcc-switchs等效项? - What is the equivalent of record-gcc-switches for Visual Studio compiler? 在什么情况下为Qt MinGW或MSVC选择什么编译器? - What compiler to choose for Qt MinGW or MSVC in what case? 哪些编译器开关使逆向工程师的生活更加艰苦/轻松? - What compiler switches make the life of a reverse engineer harder/easier? /d2vzeroupper MSVC 编译器优化标志在做什么? - What is the /d2vzeroupper MSVC compiler optimization flag doing? MSVC ++ 2008 Express Editon编译器的功能和功能 - What MSVC++ 2008 Express Editon compiler does and doesn't 什么是 -D 编译器标志 C++(clang、GNU、MSVC) - What is the -D compiler flag C++ (clang, GNU, MSVC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM