Malloc Memory corruption in C

南笙酒味 提交于 2019-12-10 14:23:11

问题


I have a problem using malloc.

I have a function called jacobi_gpu wich is called many times :

int main(int argc, char* argv[]){

    /* ... */

    int totalrot=0;
    while(nrot>0){
        iter++;
        nrot=jacobi_gpu(a,q, tol, dimmat);
        totalrot+=nrot;

        printf("iter =%3d  nrot=%3d\n",iter, nrot);
    }

    /* ... */
}

The parameters a,q,tol and dimmat are correctly initialized. A and Q are 2 square matrices and dimmat is their dimension.

Here is my 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 < dim - 1; k++){
        eye(mc, dim);
        eye(vc, dim);

        for(tid = 0; tid < 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]) > tol){

                nrot++;
                symschur2(A, dim, p, q, &c, &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;
}

My problem is in the malloc call on the mc variable :

*** glibc detected *** ./jacobi_gpu: double free or corruption (!prev): 0x00000000022944a0 ***
    *** glibc detected *** ./jacobi_gpu: malloc(): memory corruption: 0x0000000002294580 ***

Any advice?

[EDIT]

  • The function eye initializes an identity matrix
  • The function affiche 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.

More explanation

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.

Hope this time, i am more clear.

Thanks for your help


回答1:


Identity matrices are always square, but mc is not. When you call eye(mc, dim) I suspect that eye treats mc like it is a dim by dim matrix when it is in fact a 2 by dim matrix, and writes to unallocated memory.




回答2:


You are not allocating enough memory for a square matrix in your call to malloc(). The correct size would be dim squared, not just 2*dim.

This should do the trick:

mc   = (double *)malloc(dim * dim * sizeof(double));
vc   = (double *)malloc(dim * dim * sizeof(double)); 



回答3:


As far as I can tell the double free or corruption (!prev) is that you're calling free() multiple times on the same pointer, your other functions might be doing this (I suspect affiche(). Maybe try running it after export MALLOC_CHECK_=0 on your shell?




回答4:


You must have a stack corruption somewhere in your code. Compile with debug options and run your code through valgrind, it will tell you.

BTW, in C casting the result of malloc is a bad idea. Don't do that, it might hide the diagnostic of the lack of including the correct header file.



来源:https://stackoverflow.com/questions/4616503/malloc-memory-corruption-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!