[英]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个字节
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.