<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - [-cxx-abi microsoft] C++ classes are not laid out in a MS compatible way"
   href="http://llvm.org/bugs/show_bug.cgi?id=16230">16230</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[-cxx-abi microsoft] C++ classes are not laid out in a MS compatible way
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>david.majnemer@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following code:
class Foo
{
    virtual void Hello() {}

    float f;     /* 4 bytes */
};

class Bar
{
    virtual void Hello() {}

    float f;     /* 4 bytes */
    double d;    /* 8 bytes */
};

class EmptyBase
{
protected:
    virtual ~EmptyBase() {}
};

class SmallBar : public EmptyBase
{
    virtual void Hello() {}

    float f;     /* 4 bytes */
    double d;    /* 8 bytes */
};

int main() {
   return sizeof(Foo) + sizeof(Bar) + sizeof(SmallBar);
}

compiling with: clang -cc1 -x c++ -triple i386-pc-win32 -fdump-record-layouts
-cxx-abi microsoft empty.cpp gives us:

*** Dumping AST Record Layout
   0 | class Foo
   0 |   (Foo vftable pointer)
   4 |   float f
     | [sizeof=8, dsize=8, align=4
     |  nvsize=8, nvalign=4]


*** Dumping AST Record Layout
   0 | class Bar
   0 |   (Bar vftable pointer)
   4 |   float f
   8 |   double d
     | [sizeof=16, dsize=16, align=8
     |  nvsize=16, nvalign=8]


*** Dumping AST Record Layout
   0 | class EmptyBase
   0 |   (EmptyBase vftable pointer)
     | [sizeof=4, dsize=4, align=4
     |  nvsize=4, nvalign=4]


*** Dumping AST Record Layout
   0 | class SmallBar
   0 |   class EmptyBase (primary base)
   0 |     (EmptyBase vftable pointer)
   4 |   float f
   8 |   double d
     | [sizeof=16, dsize=16, align=8
     |  nvsize=16, nvalign=8]


Bizarrely, Microsoft chose to lay Bar out such that there is a bunch of padding
between the vtable pointer and the first member iff you didn't inherit from an
empty base class.

This means that we correctly calculate the size for SmallBar (16 bytes) but
incorrectly for Bar which should be 24 bytes.

The following has a more detailed (and mildly profane) write up:
<a href="http://lolengine.net/blog/2012/10/21/the-stolen-bytes">http://lolengine.net/blog/2012/10/21/the-stolen-bytes</a>

A useful document linked off the page may be:
<a href="http://www.openrce.org/articles/files/jangrayhood.pdf">http://www.openrce.org/articles/files/jangrayhood.pdf</a></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>