title: glibc detected - double free or corruption (!prev)
body: <p>I'm getting the error you may see in the title, and I can't figure out why.</p>
  
  <p>The code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;cstdlib&gt;
  #include &lt;cstdio&gt;
  
  struct CData {
      int* num;
      char* adr;
      char* ph;
  
      void (*init)(CData* owner);
      void (*del)(CData* owner);
      char* (*getAdr)(CData* owner);
      void (*setAdr)(CData* owner, char* adr);
  };
  
  void CData_init(CData* owner) {
      owner-&gt;num = (int*)malloc(sizeof(int));
      owner-&gt;adr = (char*)malloc(sizeof(char)*255);
      owner-&gt;ph = (char*)malloc(sizeof(char)*255);
  }
  
  void CData_del(CData* owner) {
      free(owner-&gt;num);
      free(owner-&gt;adr);
      free(owner-&gt;ph);
  }
  
  char* CData_getAdr(CData* owner) {
      return owner-&gt;adr;
  }
  
  void CData_setAdr(CData* owner, char* adr) {
      owner-&gt;adr = adr;
  }
  
  int main() {
      CData* data = (CData*)malloc(sizeof(CData));
  
      data-&gt;init = CData_init;
      data-&gt;del = CData_del;
      data-&gt;getAdr = CData_getAdr;
      data-&gt;setAdr = CData_setAdr;
  
      data-&gt;init(data);
  
      data-&gt;setAdr(data, "asdasd");
      printf("%d", data-&gt;getAdr(data));
  
      data-&gt;del(data);
  
      free(data); 
  
      return 0;
  }
  </code></pre>
  
  <p>So I think the error is caused by the char* manipulations but I don't know why. </p>
  
  <p>What I want to do is I want it to be possible to pass a string as a parameter to <code>data-&gt;setAdr</code> and I want it to be assigned to the variable afterwards.</p>
  
  <p>Compiling line:</p>
  
  <pre><code>gcc -o test main.cpp
  </code></pre>