<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Shimon Krokhmal's blog - Under the hood</title>
    <link>http://www.krokhmal.com/</link>
    <description>medium : .NET | JavaScript | Secure coding | Databases | Sql Server | Oracle | CodeSmith | SPS | Life</description>
    <language>en-us</language>
    <copyright>Shimon Krokhmal</copyright>
    <lastBuildDate>Sat, 21 Jul 2007 13:39:10 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>Shimonkr@gmail.com</managingEditor>
    <webMaster>Shimonkr@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.krokhmal.com/Trackback.aspx?guid=83234cdf-66bc-4a38-aad0-b9a1454af394</trackback:ping>
      <pingback:server>http://www.krokhmal.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.krokhmal.com/PermaLink,guid,83234cdf-66bc-4a38-aad0-b9a1454af394.aspx</pingback:target>
      <dc:creator>Shimon krokhmal</dc:creator>
      <wfw:comment>http://www.krokhmal.com/CommentView,guid,83234cdf-66bc-4a38-aad0-b9a1454af394.aspx</wfw:comment>
      <wfw:commentRss>http://www.krokhmal.com/SyndicationService.asmx/GetEntryCommentsRss?guid=83234cdf-66bc-4a38-aad0-b9a1454af394</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
a little background to that question,<br />
in the last week I'm doing some sort of a job interviews marathon, being asked for
some quite interesting questions (most of them are really simply OPP questions).<br />
but this one caught me off guard, wouldn't expect this one as a pre-interview question
over the phone.
</p>
        <p>
so, how does this strange creature work (which most of us using it without even knowing
what it does behind) ?
</p>
        <p>
in contrast to regular method calling, the virtual method calling isn't called directly,
instead it uses the "<a href="http://www.answers.com/topic/virtual-method-table">Virtual
Functions Table</a>". (C# compiler implements it with VFT, other compilers may
implement it via binary trees)<br />
in short, it uses pointers to functions table, to map the call to the right method.<br />
lets take a look at an example :<br /></p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Father<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">virtual</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> foo()<br />
{<br />
Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"string
from the father"</span>);<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">virtual</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> foo2()<br />
{<br />
Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"foo2
string from the father"</span>);<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">internal</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> boo()<br />
{<br />
Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"father
says boo"</span>);<br />
}<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Son
: Father<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> foo()<br />
{<br />
Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"string
from the son"</span>);<br />
}<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> moo()<br />
{<br />
Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"son
says moo"</span>);<br />
}<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> faaBase()<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.foo();<br />
}<br />
}</span>
        </p>
        <p>
we have 2 classes ,son derives from the father, and overrides one of his virtual
methods.<br /><br /></p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Father
f <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Father();<br />
Son s <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Son();<br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Father calls<br /></span><font color="#008000">// Virtual method call</font><br />
f.foo();<br />
f.foo2();<br /></span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <br />
            <font color="#008000">// Non virtual method calls</font>
            <br />
f.boo();<br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Son calls<br /></span><font color="#008000">// Virtual method call<br /></font>s.foo();<br /><br /><font color="#008000">// Non virtual method calls<br /></font>s.boo();<br />
s.moo();</span>
        </p>
        <p>
what really happens behind ?<br />
lets view a part of the disassembly code<br /></p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Virtual method call</span>
            <br />
s.foo();<br />
00000065 mov ecx,esi 
<br />
00000067 mov eax,dword ptr [ecx] 
<br /><strong>00000069 call dword ptr [eax+38h]</strong><br />
0000006c nop 
<br /><br />
s.foo2();<br />
0000006d mov ecx,esi 
<br />
0000006f mov eax,dword ptr [ecx] 
<br /><strong>00000071 call dword ptr [eax+3Ch]</strong><br />
00000074 nop 
<br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//
Non virtual method calls</span><br />
s.boo();<br />
00000075 mov ecx,esi 
<br />
00000077 cmp dword ptr [ecx],ecx 
<br /><strong>00000079 call FFAB30A0</strong><br />
0000007e nop 
<br /><br />
s.moo();<br />
0000007f mov ecx,esi 
<br />
00000081 cmp dword ptr [ecx],ecx 
<br /><strong>00000083 call FFAB3158</strong><br />
00000088 nop </span>
        </p>
        <p>
          <br />
we can see clearly that the non virtual call has a direct calling to the function
(a hard coded address),<br />
whereas the virtual method calling points to the virtual method table that resides
in the Son object.<br />
lets take a look on that table :
</p>
        <p class="HighlightedCode">
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EEClass:
00a21370<br />
Module: 00a22c24<br />
Name: VTF.Son<br />
mdToken: 02000004 (E:\PROJECTS\vtf 2005\VTF\VTF\bin\Debug\VTF.exe)<br />
BaseSize: 0xc<br />
ComponentSize: 0x0<br />
Number of IFaces <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> IFaceMap:
0<br />
Slots <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> VTable:
9<br />
--------------------------------------<br />
MethodDesc Table<br />
Entry    MethodDesc JIT     Name<br />
7934cdcc 79137ab8    PreJIT System.Object.ToString()<br />
7934bba0 79137ac0    PreJIT System.Object.Equals(System.Object)<br />
7934bb90 79137ad8    PreJIT System.Object.GetHashCode()<br />
793424c0 79137ae0    PreJIT System.Object.Finalize()<br /><strong>00a231b8 00a23140    JIT    VTF.Son.foo()<br />
00a23100 00a23088    JIT    VTF.Father.foo2()</strong><br />
00a231c8 00a23148    NONE   VTF.Son.moo()<br />
00a231d8 00a23150    NONE   VTF.Son.faaBase()<br />
00a231e8 00a23158    JIT    VTF.Son..ctor()</span>
        </p>
        <p>
 
</p>
        <p>
we can see a couple of things from this table:
</p>
        <ul>
          <li>
VT lists all the methods the son object holds 
</li>
          <li>
VT lists the virtual functions of the father that are virtual 
</li>
          <li>
When the son object overrides one of the virtual methods that the father implements, 
<br />
the father method entry is being replaced by the new son method (line 5 at the table
- Son.foo()) 
</li>
          <li>
the VT does not list father method that are not virtual.</li>
        </ul>
        <p>
actually the Son.moo() method looks a little unnecessary in the VT due to the
fact that the function is not virtual and will be addressed directly and not by the
VT.
</p>
        <p>
in conclusion, think twice before you declare a method as virtual, because it contains
some performance hit
</p>
        <img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=83234cdf-66bc-4a38-aad0-b9a1454af394" />
        <br />
        <hr />
Shimon krokhmal, a part of the Krokhmal family</body>
      <title>Virtual functions under the hood</title>
      <guid isPermaLink="false">http://www.krokhmal.com/PermaLink,guid,83234cdf-66bc-4a38-aad0-b9a1454af394.aspx</guid>
      <link>http://www.krokhmal.com/2007/07/21/VirtualFunctionsUnderTheHood.aspx</link>
      <pubDate>Sat, 21 Jul 2007 13:39:10 GMT</pubDate>
      <description>&lt;p&gt;
a little background to that question,&lt;br&gt;
in the last week I'm doing some sort of a job interviews marathon, being asked for
some quite interesting questions (most of them are really simply OPP questions).&lt;br&gt;
but this one caught me off guard, wouldn't expect this one as a pre-interview question
over the phone.
&lt;/p&gt;
&lt;p&gt;
so, how does this strange creature work (which most of us using it without even knowing
what it does behind) ?
&lt;/p&gt;
&lt;p&gt;
in contrast to regular method calling, the virtual method calling isn't called directly,
instead it uses the "&lt;a href="http://www.answers.com/topic/virtual-method-table"&gt;Virtual
Functions Table&lt;/a&gt;". (C# compiler implements it with VFT, other compilers&amp;nbsp;may
implement it via binary trees)&lt;br&gt;
in short, it uses pointers to functions table, to map the call to the right method.&lt;br&gt;
lets take a look at an example :&lt;br&gt;
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Father&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;virtual&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; foo()&lt;br&gt;
{&lt;br&gt;
Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"string
from the father"&lt;/span&gt;);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;virtual&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; foo2()&lt;br&gt;
{&lt;br&gt;
Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"foo2
string from the father"&lt;/span&gt;);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;internal&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; boo()&lt;br&gt;
{&lt;br&gt;
Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"father
says boo"&lt;/span&gt;);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Son
: Father&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; foo()&lt;br&gt;
{&lt;br&gt;
Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"string
from the son"&lt;/span&gt;);&lt;br&gt;
}&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; moo()&lt;br&gt;
{&lt;br&gt;
Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"son
says moo"&lt;/span&gt;);&lt;br&gt;
}&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; faaBase()&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.foo();&lt;br&gt;
}&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
we have 2 classes ,son&amp;nbsp;derives from the father, and overrides one of his virtual
methods.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Father
f &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Father();&lt;br&gt;
Son s &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Son();&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Father calls&lt;br&gt;
&lt;/span&gt;&lt;font color=#008000&gt;// Virtual method call&lt;/font&gt;
&lt;br&gt;
f.foo();&lt;br&gt;
f.foo2();&lt;br&gt;
&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;
&lt;br&gt;
&lt;font color=#008000&gt;// Non virtual method calls&lt;/font&gt;
&lt;br&gt;
f.boo();&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Son calls&lt;br&gt;
&lt;/span&gt;&lt;font color=#008000&gt;// Virtual method call&lt;br&gt;
&lt;/font&gt;s.foo();&lt;br&gt;
&lt;br&gt;
&lt;font color=#008000&gt;// Non virtual method calls&lt;br&gt;
&lt;/font&gt;s.boo();&lt;br&gt;
s.moo();&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
what really happens behind ?&lt;br&gt;
lets view a part of the disassembly code&lt;br&gt;
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Virtual method call&lt;/span&gt;
&lt;br&gt;
s.foo();&lt;br&gt;
00000065 mov ecx,esi 
&lt;br&gt;
00000067 mov eax,dword ptr [ecx] 
&lt;br&gt;
&lt;strong&gt;00000069 call dword ptr [eax+38h]&lt;/strong&gt; 
&lt;br&gt;
0000006c nop 
&lt;br&gt;
&lt;br&gt;
s.foo2();&lt;br&gt;
0000006d mov ecx,esi 
&lt;br&gt;
0000006f mov eax,dword ptr [ecx] 
&lt;br&gt;
&lt;strong&gt;00000071 call dword ptr [eax+3Ch]&lt;/strong&gt; 
&lt;br&gt;
00000074 nop 
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//
Non virtual method calls&lt;/span&gt;
&lt;br&gt;
s.boo();&lt;br&gt;
00000075 mov ecx,esi 
&lt;br&gt;
00000077 cmp dword ptr [ecx],ecx 
&lt;br&gt;
&lt;strong&gt;00000079 call FFAB30A0&lt;/strong&gt; 
&lt;br&gt;
0000007e nop 
&lt;br&gt;
&lt;br&gt;
s.moo();&lt;br&gt;
0000007f mov ecx,esi 
&lt;br&gt;
00000081 cmp dword ptr [ecx],ecx 
&lt;br&gt;
&lt;strong&gt;00000083 call FFAB3158&lt;/strong&gt; 
&lt;br&gt;
00000088 nop &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
we can see clearly that the non virtual call has a direct calling to the function
(a hard coded address),&lt;br&gt;
whereas the virtual method calling points to the virtual method table that resides
in the Son object.&lt;br&gt;
lets take a look on that table :
&lt;/p&gt;
&lt;p class=HighlightedCode&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EEClass:
00a21370&lt;br&gt;
Module: 00a22c24&lt;br&gt;
Name: VTF.Son&lt;br&gt;
mdToken: 02000004 (E:\PROJECTS\vtf 2005\VTF\VTF\bin\Debug\VTF.exe)&lt;br&gt;
BaseSize: 0xc&lt;br&gt;
ComponentSize: 0x0&lt;br&gt;
Number of IFaces &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; IFaceMap:
0&lt;br&gt;
Slots &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; VTable:
9&lt;br&gt;
--------------------------------------&lt;br&gt;
MethodDesc Table&lt;br&gt;
Entry&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MethodDesc JIT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Name&lt;br&gt;
7934cdcc 79137ab8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PreJIT System.Object.ToString()&lt;br&gt;
7934bba0 79137ac0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PreJIT System.Object.Equals(System.Object)&lt;br&gt;
7934bb90 79137ad8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PreJIT System.Object.GetHashCode()&lt;br&gt;
793424c0 79137ae0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PreJIT System.Object.Finalize()&lt;br&gt;
&lt;strong&gt;00a231b8 00a23140&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JIT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;VTF.Son.foo()&lt;br&gt;
00a23100 00a23088&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JIT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;VTF.Father.foo2()&lt;/strong&gt;
&lt;br&gt;
00a231c8 00a23148&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NONE&amp;nbsp;&amp;nbsp;&amp;nbsp;VTF.Son.moo()&lt;br&gt;
00a231d8 00a23150&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NONE&amp;nbsp;&amp;nbsp;&amp;nbsp;VTF.Son.faaBase()&lt;br&gt;
00a231e8 00a23158&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JIT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;VTF.Son..ctor()&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
we can see a couple of things from this table:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
VT lists all the methods the son object holds 
&lt;li&gt;
VT&amp;nbsp;lists the virtual functions of the father that are virtual 
&lt;li&gt;
When the son object overrides one of the virtual methods that the father implements, 
&lt;br&gt;
the father method entry is being replaced by the new son method (line 5 at the table
- Son.foo()) 
&lt;li&gt;
the VT does not list father method that are not virtual.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
actually the Son.moo() method looks a little unnecessary in the VT&amp;nbsp;due to the
fact that the function is not virtual and will be addressed directly and not by the
VT.
&lt;/p&gt;
&lt;p&gt;
in conclusion, think twice before you declare a method as virtual, because it contains
some performance hit
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.krokhmal.com/aggbug.ashx?id=83234cdf-66bc-4a38-aad0-b9a1454af394" /&gt;
&lt;br /&gt;
&lt;hr /&gt;Shimon krokhmal, a part of the Krokhmal family</description>
      <comments>http://www.krokhmal.com/CommentView,guid,83234cdf-66bc-4a38-aad0-b9a1454af394.aspx</comments>
      <category>.Net</category>
      <category>job interviews</category>
      <category>Under the hood</category>
    </item>
  </channel>
</rss>