title: Malloc Memory corruption in C
body: <p>I have a problem using malloc. </p>
  
  <p>I have a function called <code>jacobi_gpu</code> wich is called many times : </p>
  
  <pre><code>int main(int argc, char* argv[]){
  
      /* ... */
  
      int totalrot=0;
      while(nrot&gt;0){
          iter++;
          nrot=jacobi_gpu(a,q, tol, dimmat);
          totalrot+=nrot;
  
          printf("iter =%3d  nrot=%3d\n",iter, nrot);
      }
  
      /* ... */
  }
  </code></pre>
  
  <p>The parameters a,q,tol and dimmat are correctly initialized. 
  A and Q are 2 square matrices and dimmat is their dimension. </p>
  
  <p>Here is my code :</p>
  
  <pre><code>int jacobi_gpu(double A[], double Q[], double tol, long int dim){
      int nrot, p, q, k, tid;
      double c, s;
      double *mc, *vc;
  
      printf("jacobi begins \n");
  
      mc   = (double *)malloc(2 * dim * sizeof(double));
      vc   = (double *)malloc(2 * dim * sizeof(double));
  
      if( mc == NULL || vc == NULL){
          fprintf(stderr, "pb allocation matricre\n");
          exit(1);
      }
  
      nrot = 0;
  
      for(k = 0; k &lt; dim - 1; k++){
          eye(mc, dim);
          eye(vc, dim);
  
          for(tid = 0; tid &lt; floor(dim /2); tid++){
              p = (tid + k)%(dim - 1);
              if(tid != 0)
                  q = (dim - tid + k - 1)%(dim - 1);
              else
                  q = dim - 1;
  
              //printf("p = %d | q = %d\n", p, q);
              if(fabs(A[p + q*dim]) &gt; tol){
  
                  nrot++;
                  symschur2(A, dim, p, q, &amp;c, &amp;s);
  
                  mc[2*tid] = p;        vc[2 * tid] = c;
                  mc[2*tid + 1] = q;    vc[2*tid + 1] = -s;
  
                  mc[2*tid + 2*(dim - 2*tid) - 2] = p;
                  vc[2*tid + 2*(dim - 2*tid)   - 2 ] = s;
  
                  mc[2*tid + 2*(dim - 2*tid) - 1] = q;
                  vc[2 * tid + 2*(dim - 2*tid) - 1 ] = c;     
              }
          }
  
          affiche(mc,dim,2,"Matrice creuse");
          affiche(vc,dim,2,"Valeur creuse");
  
      }
      printf("end\n");
      free(mc);
      free(vc);
      return nrot;
  }
  </code></pre>
  
  <p>My problem is in the malloc call on the mc variable : </p>
  
  <pre><code>*** glibc detected *** ./jacobi_gpu: double free or corruption (!prev): 0x00000000022944a0 ***
      *** glibc detected *** ./jacobi_gpu: malloc(): memory corruption: 0x0000000002294580 ***
  </code></pre>
  
  <p>Any advice?</p>
  
  <p>[EDIT]</p>
  
  <ul>
  <li>The function <strong>eye</strong> initializes an identity matrix</li>
  <li>The function <strong>affiche</strong> displays the matrix with lines and columns. The first parameter is the matrix, the second is the number of lines and the third one is the number of column.</li>
  </ul>
  
  <h2>More explanation</h2>
  
  <p>The purpose of the matrix mc is to store the variables p and q. Those variables contains column indices. 
  The purpose of the matrix vc is to store the values contained in those column. 
  For instance, if the first line of the matrix mc is 0 and 5 ( p = 0, q = 5), that means that the values in the matrix vc will be in the column 0 and 5.
    If the matrix fifth line in the matrix mc is 2 3 ( p = 2, q = 3), that means that the values in the fifth line in vc will be in column 2 and 3.</p>
  
  <p>Hope this time, i am more clear. </p>
  
  <p>Thanks for your help</p>