views:

242

answers:

4

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.


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.


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.

+1  A: 

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.

Type definitions can be affected by different files #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.

peterchen
+1  A: 

#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.

Michael Burr
+2  A: 
  • 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?
RED SOFT ADAIR
+5  A: 

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

shoosh
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.
Michael Burr