title: error in C using malloc : corrupted size vs prev_size
body: <p>I found an answer for python but I didn't understand it.</p>
  
  <p>The code is a modified merge sort. It is working fine for a small number of inputs I checked upto 10. But when I run it through an online judge, when number of inputs were high (500) it gave me this error:</p>
  
  <pre><code>Error in 'a.out': corrupted size vs. prev_size: 0x0000000000d5b8b0
  ======= Backtrace: =========
  /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3b83a5b7e5]
  /lib/x86_64-linux-gnu/libc.so.6(+0x80dfb)[0x7f3b83a64dfb]
  /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3b83a6853c]
  a.out[0x4009d1]
  a.out[0x400ac7]
  a.out[0x400a87]
  a.out[0x400aa4]
  a.out[0x400a87]
  a.out[0x400bc7]
  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3b83a04830]
  a.out[0x4005b9]
  ======= Memory map: ========
  </code></pre>
  
  <p>and it goes for another 15 lines. Why am I getting this error? Is it because of some mistake I am making during dynamic allocation of memory using <code>malloc</code>?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void *Merge(int *A,int l,int m,int r,int *B,int *F);
  void *Merge(int *A,int l,int m,int r,int *B,int *F){
    int i=l,j=m,k=0,*C,x,y=l,z,cSize,temp,*D,*E;
  
    cSize = r-l;
    C = (int *) malloc (cSize * sizeof(int));
    D = (int *) malloc (cSize * sizeof(int));
    E = (int *) malloc (cSize * sizeof(int));
  
    while (k &lt; cSize){
      if((j==r) || ((i!=m) &amp;&amp; ((A[j]*B[i]) &gt;= (A[i]*B[j])))){
          C[k] = A[i];
          D[k] = B[i];
          E[k] = F[i];
          i++;
          k++;
      }
      if((i&gt;=m) || ((j!=r) &amp;&amp; ((A[j]*B[i]) &lt; (A[i]*B[j])))){
          C[k] = A[j];
          D[k] = B[j];
          E[k] = F[j];
          j++;
          k++;
      }
    }
    for(x=0;x&lt;k;x++){
      A[y] = C[x];
      B[y] = D[x];
      F[y] = E[x];
      y++;
    }
    free(C);
    free(D);
    free(E);
  }
  
  void *MergeSort(int *A,int left,int right,int *B,int *C);
  void *MergeSort(int *A,int left,int right,int *B,int *C){
    int mid,i,j,k=0,l=0,*R,*L;
    if(right - left == 1){
      A[left] = A[left];
    }
    if(right-left &gt; 1){
      mid = (left+right)/2;
  
      MergeSort(A,left,mid,B,C);
      MergeSort(A,mid,right,B,C);
  
      Merge(A,left,mid,right,B,C);
    }
  }
  
  int main(){
    int n,i=0,newNumt,newNumo,*a,*b,*c;
  
    scanf("%d",&amp;n);
    a = (int *) malloc (n * sizeof(int));
    b = (int *) malloc (n * sizeof(int));
    c = (int *) malloc (n * sizeof(int));
  
    for(i=0;i&lt;n;i++){
      scanf("%d %d",&amp;a[i],&amp;b[i]);
      c[i]= i+1;
    }
    MergeSort(a,0,n,b,c);
    for(i=0;i&lt;n;i++){
      printf("%d\n",c[i]);
    }
  
    return 0;
  }
  </code></pre>