Note that there are some explanatory texts on larger screens.

plurals
  1. POwhere did my 4 bytes go?
    text
    copied!<pre><code>#include &lt;iostream&gt; #include &lt;cstdlib&gt; using std::cout; class A { public : A() { cout &lt;&lt; "A()" &lt;&lt; this &lt;&lt; "\n";} ~A() { cout &lt;&lt; "~A()" &lt;&lt; this &lt;&lt; "\n";} //void func() { } virtual void debug(int a) { cout &lt;&lt; "A::debug";} private : int a; }; class A1 : public A { public : A1() { cout &lt;&lt; "A1()"&lt;&lt; this &lt;&lt; "\n";} ~A1() { cout &lt;&lt; "~A1()"&lt;&lt; this &lt;&lt; "\n";} private : int a1; }; class A2 : public A { public : A2() { cout &lt;&lt; "A2()"&lt;&lt; this &lt;&lt; "\n";} ~A2() { cout &lt;&lt; "~A2()"&lt;&lt; this &lt;&lt; "\n";} private : int a2; }; class B : public A1, public A2 { public : B() { cout &lt;&lt; "B()"&lt;&lt; this &lt;&lt; "\n";} ~B() { cout &lt;&lt; "~B()"&lt;&lt; this &lt;&lt; "\n";} void debug() { cout &lt;&lt; "B::debug()"; } private : int a3; }; int main() { cout &lt;&lt; "sizeof(int)" &lt;&lt; sizeof(int) &lt;&lt; "\n"; cout &lt;&lt; "sizeof(void*)" &lt;&lt; sizeof(void*) &lt;&lt; "\n"; cout &lt;&lt; "sizeof(A): " &lt;&lt; sizeof(A) &lt;&lt; "\n"; cout &lt;&lt; "sizeof(A1): " &lt;&lt; sizeof(A1) &lt;&lt; "\n"; cout &lt;&lt; "sizeof(A2): " &lt;&lt; sizeof(A2) &lt;&lt; "\n"; cout &lt;&lt; "sizeof(B): " &lt;&lt; sizeof(B) &lt;&lt; "\n"; B b; b.debug(); } </code></pre> <p>output :</p> <pre><code>sizeof(int)4 sizeof(void*)4 sizeof(A): 8 sizeof(A1): 12 sizeof(A2): 12 **sizeof(B): 28** A()0x28fef4 A1()0x28fef4 **A()0x28ff00** A2()0x28ff00 B()0x28fef4 B::debug()~B()0x28fef4 ~A2()0x28ff00 ~A()0x28ff00 ~A1()0x28fef4 ~A()0x28fef4 </code></pre> <p>Both A1 and A2 are 4(vtbl) + 4(A'sint) + 4(respective int) = 12 bytes but B is 28 bytes I know its not guaranteed but what could be the possible use of those 4 bytes...I dont see any padding issues ? Can anyone point out what am I missing ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload