简体   繁体   English

C中的Malloc内存损坏

[英]Malloc Memory corruption in C

I have a problem using malloc. 我使用malloc时遇到问题。

I have a function called jacobi_gpu wich is called many times : 我有一个名为jacobi_gpu的函数,它被多次调用:

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,q,tol和dimmat被正确初始化。 A and Q are 2 square matrices and dimmat is their dimension. A和Q是2平方矩阵,dimmat是它们的维度。

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 : 我的问题是在moc变量上的malloc调用:

*** 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. 函数affiche用行和列显示矩阵。 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. 矩阵mc的目的是存储变量p和q。 Those variables contains column indices. 这些变量包含列索引。 The purpose of the matrix vc is to store the values contained in those column. 矩阵vc的目的是存储这些列中包含的值。 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. 例如,如果矩阵mc的第一行是0和5(p = 0,q = 5),这意味着矩阵vc中的值将在第0列和第5列中。如果矩阵中的第五行是矩阵mc是2 3(p = 2,q = 3),这意味着vc中第五行中的值将在第2列和第3列中。

Hope this time, i am more clear. 希望这一次,我更清楚。

Thanks for your help 谢谢你的帮助

Identity matrices are always square, but mc is not. 身份矩阵总是正方形,但mc不是。 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. 当你调用eye(mc, dim)我怀疑眼睛对待mc就像它是暗淡的矩阵,当它实际上是一个2乘暗的矩阵,并写入未分配的内存。

You are not allocating enough memory for a square matrix in your call to malloc() . 在调用malloc()没有为方阵分配足够的内存。 The correct size would be dim squared, not just 2*dim . 正确的大小将是dim平方,而不仅仅是2*dim

This should do the trick: 这应该做的伎俩:

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

据我所知, double free or corruption (!prev)是你在同一个指针上多次调用free(),你的其他函数可能正在这样做(我怀疑affiche() 。也许尝试运行之后)在shell上export MALLOC_CHECK_=0

You must have a stack corruption somewhere in your code. 您的代码中必须存在堆栈损坏。 Compile with debug options and run your code through valgrind, it will tell you. 通过valgrind编译调试选项并运行代码,它会告诉你。

BTW, in C casting the result of malloc is a bad idea. 顺便说一句,在C语言中, malloc的结果是一个坏主意。 Don't do that, it might hide the diagnostic of the lack of including the correct header file. 不要这样做,它可能会隐藏缺少包括正确头文件的诊断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM