gpt4 book ai didi

MSVC: what compiler switches affect the size of structs?(MSVC:哪些编译器开关会影响结构的大小?)

转载 作者:bug小助手 更新时间:2023-10-25 13:04:18 26 4
gpt4 key购买 nike



I have two DLLs compiled separately, one is compiled from Visual Studio 2008 and one is a mex file compiled from matlab.

Both DLLs have a header file which they include. 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.

What other compiler switches may affect the size of a struct?

The struct is a simple POCO with no inheritance and no virtual functions.

我有两个单独编译的DLL,一个是从Visual Studio2008编译的,另一个是从MATLAB编译的MEX文件。这两个DLL都包含一个头文件。当我获取一个DLL中的结构sizeof()时,它返回48,而在另一个DLL中,它返回64。我已经检查了/ZP开关,并且在两个编译中都将其设置为/Zp8。还有哪些编译器开关可能会影响结构的大小?该结构是一个简单的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.

I can tell matlab the exact compiler options to use when compiling the mex so it's not it.

在调试中,sizeof()在两种情况下都返回56,在发布中,在MEX编译中返回48,而不是64。我可以告诉matlab在编译mex时要使用的确切编译器选项,所以不是这样的。






Edit

编辑



After checking with offsetof, it turns out that the difference is in the size of the std::vector. in one dll it's 32 and in the other it's 48.

Both dlls are x64.

在使用offsetof进行检查之后,结果发现差别在于std::向量的大小。在一个DLL中是32,在另一个中是48。这两个DLL都是x64。


更多回答

Are you sure there are no pragmas that change the packing when one or the other library is being built?

您确定在构建一个或另一个库时不存在更改打包的编译指示吗?

Also, show the code for the struct - it may give a clue.

此外,显示结构的代码-它可能会给出线索。

Do the matlab docs suggest what struct alignment they're using? It might be that you might have to use the appropriate #pragmas to tweak the alignment on the C++ side.

MatLab文档是否建议使用哪种结构对齐?可能需要使用适当的#杂注来调整C++端的对齐方式。

In MSVC 2008 the vector is 3 pointers and an allocator - I'd look at what might be different in the allocator.

在MSVC 2008中,向量是3个指针和一个分配器--我会看看分配器有什么不同。

优秀答案推荐

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'

This in turn causes a difference of 16 bytes in the size of std::vector

好了,这可能是有史以来最晦涩难懂的事情了。结果是,MatLab将/D_SECURE_SCL=0添加到编译中,这会禁用名为‘Secure Iterator’的东西,这又会导致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?



Target platform (e.g. 32bit vs. 64 bit)
Also /Zp can be overridden by #pragma statements.
If you use pointers to members in the struct, the /vm? option affects their size.

目标平台(例如32位vs. 64位)/Zp也可以被#pragma语句覆盖。如果在结构体中使用指向成员的指针,那么/vm?选择会影响其大小。



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.

当构建一个或另一个库时,可能会使用#杂注包,该库可能会改变结构的大小,而与编译器的命令行上的内容无关。



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定义为一种或另一种类型。



In my case _HAS_EXCEPTIONS=0 was causing the sizeof(std::exception) to go down from 24 bytes to 16 bytes.

在我的例子中,_HAS_EXCEPTIONS=0导致sizeof(std::Except)从24字节减少到16字节。


更多回答

I thought it might be something like that, but I found _HAS_ITERATOR_DEBUGGING which is always 0 for release builds. It looks like the 'checked iterator' stuff enabled by _SECURE_SCL can be enabled for release builds, too.

我想它可能是类似的东西,但我发现_HAS_ITERATOR_DEBUGGING对于发布版本总是0。看起来由_SECURE_SCL启用的“检查迭代器"也可以为发布版本启用。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com