用C的malloc内存损坏 [英] Malloc Memory corruption in C

查看:145
本文介绍了用C的malloc内存损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用malloc问题。

I have a problem using malloc.

我有一个名为 jacobi_gpu 至极函数被调用很多次:

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);
    }

    /* ... */
}

参数A,Q,TOL和dimmat被正确初始化。
A和Q为2方阵和dimmat是他们的维度。

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

下面是我的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;
}

我的问题是在MC变量malloc调用:

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 ***

任何意见?


  • 函数的初始化一个单位矩阵

  • 函数的企业公告显示与行和列的矩阵。第一个参数是矩阵,第二个是行数,第三个是列的数目。

  • 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.

矩阵MC的目的是存储变量p和q。这些变量包含列的索引。
基质的vc的目的是存储包含在那些列中的值。
例如,如果矩阵MC的第一行是0和5(p = 0时,Q = 5),这意味着,在基体的vc中的值将是在列0和5。
  如果在矩阵MC矩阵第五行是2 3(p值= 2,q = 3的),这意味着,在在vc第五行的值将是在列2和3

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.

感谢您的帮助。

推荐答案

单位矩阵始终为方形,但三菱商事是没有的。当你调用眼(MC,DIM)我怀疑眼对待MC喜欢它是一个暗淡在昏暗的矩阵时,它实际上是一个2在昏暗的矩阵,写入未分配的内存。

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.

这篇关于用C的malloc内存损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆