title: Unknown segmentation fault involving vtable lookup
body: <p>So I messing around with virtual functions, trying to find a way to mitigate their cost, and I encountered an entirely unknown error. My entire code follows;</p>
  
  <pre class="lang-C++ prettyprint-override"><code>#include &lt;iostream&gt;
      #include &lt;cmath&gt;
  
      class Base
      {
      public:
          virtual void func() = 0;
      };
  
      class Der1 : public Base
      {
      public:
          virtual void func();
      };
      void Der1::func(){std::cout &lt;&lt; "I am Der1\n";}
  
      class Der2 : public Base
      {
      public:
          virtual void func();
      };
      void Der2::func(){std::cout &lt;&lt; "I am Der2\n";}
  
      void myFornction(Base* B){
          std::cout &lt;&lt; "Within Fornction " &lt;&lt; B &lt;&lt; '\n';
          for (int i = 0; i &lt; 10; i++)
              B-&gt;func();
      }
  
      using namespace std;
  
      int main()
      {
          Der2* B;
          std::cout &lt;&lt; "Actual Address " &lt;&lt; B &lt;&lt; '\n';
          myFornction(B);
          return 0;
      }
  </code></pre>
  
  <p>this was compiled with the GCC compiler that was included with codeblocks (unfortunately the version is unlisted), and here is the assembly of the function at fault;</p>
  
  <pre class="lang-Cpp prettyprint-override"><code>24  void myFornction(Base* B){
  0x00401373  push   %ebp
  0x00401374  mov    %esp,%ebp
  0x00401376  sub    $0x28,%esp
  25      std::cout &lt;&lt; "Within Fornction " &lt;&lt; B &lt;&lt; '\n';
  0x00401379  movl   $0x46e03a,0x4(%esp)
  0x00401381  movl   $0x477860,(%esp)
  0x00401388  call   0x468e68 &lt;std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*)&gt;
  0x0040138D  mov    0x8(%ebp),%edx
  0x00401390  mov    %edx,(%esp)
  0x00401393  mov    %eax,%ecx
  0x00401395  call   0x449524 &lt;std::ostream::operator&lt;&lt;(void const*)&gt;
  0x0040139A  sub    $0x4,%esp
  0x0040139D  movl   $0xa,0x4(%esp)
  0x004013A5  mov    %eax,(%esp)
  0x004013A8  call   0x468f44 &lt;std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char)&gt;
  26      for (int i = 0; i &lt; 10; i++)
  0x004013AD  movl   $0x0,-0xc(%ebp)
  0x004013B4  jmp    0x4013c5 &lt;myFornction(Base*)+82&gt;
  0x004013C2  incl   -0xc(%ebp)
  0x004013C5  cmpl   $0x9,-0xc(%ebp)
  0x004013C9  setle  %al
  0x004013CC  test   %al,%al
  0x004013CE  jne    0x4013b6 &lt;myFornction(Base*)+67&gt;
  27          B-&gt;func();
  0x004013B6  mov    0x8(%ebp),%eax
  0x004013B9  mov    (%eax),%eax
  0x004013BB  mov    (%eax),%eax /// The program fails with a segmentation fault here
  0x004013BD  mov    0x8(%ebp),%ecx
  0x004013C0  call   *%eax
  28  }
  0x004013D0  leave
  0x004013D1  ret
  </code></pre>
  
  <p>The program exits with a segmentation fault when run, looking at the assembly I can tell that the seg fault happens during look up, but I cannot think of a reason or resolution for this. Also, it is kind of random that this happens now, as I have been working with some of my own virtual classes for sometime now with no problems. Why now? And why would this specific code generate a fault?</p>