title: error: Cannot access memory at address
body: <p>I'm trying to return pointer from function in derived class,
  This is the code:</p>
  
  <p>class A</p>
  
  <pre><code>class A { 
   protected:
    C c;
  
   public:
    virtual void func(){
     unsigned char *data;
     int size=getData(data);
    }
  }
  </code></pre>
  
  <p>class B</p>
  
  <pre><code>class B : public A {
  
  private:
   int size;
  public:
   B(const C &amp;_c) { c=_c; };
   const int B::getData(unsigned char *data) const {
      data=(unsigned char *)malloc(size);
      memcpy(data,c.getMem(),size);
      if(!data){
          //err
      }
      return size;
  }
  </code></pre>
  
  <p>class C</p>
  
  <pre><code>class C {
   private:
    unsigned char *mem;
   public:
    C(unsigned char *_mem) : mem(_mem);
    const unsigned char *getMem() const { return mem; };
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>C c(...);
  B *b=new B(c);
  b-&gt;func();
  </code></pre>
  
  <p>The error I get when getData returns</p>
  
  <pre><code>(this=0x50cdf8, data=0x2 &lt;error: Cannot access memory at address 0x2&gt;, size=32)
  </code></pre>
  
  <p>Thanks.</p>