Boost logo

Boost :

Subject: Re: [boost] [optional] memory use for optional refs and ptrs
From: Stephan T. Lavavej (stl_at_[hidden])
Date: 2010-10-06 19:47:29


[Sebastian Redl]
> Anyway, I can further state with conviction that no compiler for non-embedded platforms
> changes its ABI based on compiler options, except perhaps a compiler option intended
> to do specifically that. (Clang has an option to switch between various ABIs.) But no
> debug or optimization option will change the object layout algorithm. To do so would
> be pure insanity, because it would mean that you cannot link debug versions of code
> with non-debug versions, optimized versions with unoptimized ones.
> Well, MSVC might do it. But even there I very much doubt it.

Here's an extremely useful undocumented option:

C:\Temp>type meow.cpp
struct Foo {
  double d;
  char c;
  // expect 7 bytes padding
};

static_assert(sizeof(Foo) == 16, "Bad size for Foo");

struct Bar {
  Foo i;
  short s; // placed in padding?
};

C:\Temp>cl /EHsc /nologo /W4 /c /d1reportSingleClassLayoutFoo meow.cpp
meow.cpp
class Foo size(16):
        +---
 0 | d
 8 | c
        | <alignment member> (size=7)
        +---

C:\Temp>cl /EHsc /nologo /W4 /c /d1reportSingleClassLayoutBar meow.cpp
meow.cpp
class Bar size(24):
        +---
 0 | Foo i
16 | s
        | <alignment member> (size=6)
        +---

MSVC does have options that affect layout (most notoriously, /Zp changes packing, and then there are the obscure options like /vd2 and /vmg), but optimizations don't.

Debug versus release mode (/MTd versus /MT, or /MDd versus /MD) affects which Standard Library implementation you link to. By default, the IDE compiles in debug mode without optimizations, and in release mode with optimizations, but they are not inherently tied together. Mixing debug and release mode will usually trigger linker errors, and it's an extremely bad idea, because the representations of Standard Library objects like vector are sensitive to debug versus release - but the library is choosing to add or remove data members, the compiler's layout algorithm is not changing.

Stephan T. Lavavej
Visual C++ Libraries Developer


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk