title: In C++ should derived class pointer point to base class pointer?
body: <p>I've very rare error and to understand the reason I need to know some facts of C++. Let me explain the problem:</p>
  
  <p>There is such structure:</p>
  
  <pre><code>struct Base;                 // not polimorfic
  class Derived : public Base; // has pure virtual
  class ImplDerived : public Derived    // implements pure virtuals
  
  some_function(Base* base);
  int main() {
      ImplDerived impd;
      some_function(&amp;impd);
  }
  </code></pre>
  
  <p>Now, what's happening is this:</p>
  
  <ul>
  <li><p>Running on <strong>desktop linux</strong> environment:</p>
  
  <p>No error.</p>
  
  <p>Inside <code>some_function</code>, <strong>base</strong> pointer points to the same address of <strong>&amp;impd</strong>. In other words, in memory, ImplDerived object starts with <code>Base</code> object. i.e, when I'm debugging, I see this:</p>
  
  <pre><code>ImplDerived  : 0xab00
  
    -&gt; Derived : 0xab00
  
      -&gt; Base  : 0xab00
  </code></pre></li>
  <li><p>Running on <strong>embedded linux</strong> in embedded device (compiled with its compiler):</p>
  
  <p>Error.</p>
  
  <p>Inside <code>some_function</code>, <strong>base</strong> pointer points again to <strong>&amp;impd</strong>. </p>
  
  <p>However, this time it is not the right place where <strong>base</strong> is. Because now<br>
  <strong>base</strong> starts at <strong>&amp;impd</strong> <strong>+4</strong> bytes.</p>
  
  <p>When I'm debugging I see this:</p>
  
  <pre><code>ImplDerived  : 0xab00
  
    -&gt; Derived : 0xab00
  
      -&gt; Base  : 0xab04
  </code></pre></li>
  </ul>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li><p>AFAIK, the offset between base and derived pointer is permittible, <strong>right</strong>?</p></li>
  <li><p>If it is, then should not the implicit casting when passing the pointer to the <code>some_function</code> find where the <code>base</code> is?</p></li>
  <li><p>If it should, then would it be some error in compilation?</p></li>
  </ul>
  
  <p>Thanks.</p>
  
  <p><strong>Compilers:</strong></p>
  
  <ul>
  <li>Desktop: gcc 4.9.2-10 Debian 64 bits</li>
  <li>Embedded: gcc 4.8.0</li>
  </ul>
  
  <p><strong>EDIT (RESULT):</strong></p>
  
  <p>After all tests, we have decided that this is a compiler error. Casting just does not work correctly, i) for non-polymorphic base, ii) for multiple inheritance.</p>