Valgrind无效写

时间:2014-06-14 07:07:44

标签: c valgrind

==3905== ERROR SUMMARY: 14 errors from 2 contexts (suppressed: 2 from 2)
==3905== 
==3905== 6 errors in context 1 of 2:
==3905== Invalid write of size 4
==3905==    at 0x401BFE: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905==    by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905==    by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905==    by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905==  Address 0x51fc724 is 36 bytes inside a block of size 39 alloc'd
==3905==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3905==    by 0x401064: alloc_matrix (in /home/suraj/Desktop/project/fm)
==3905==    by 0x401A59: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905==    by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905==    by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905==    by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905== 
==3905== 
==3905== 8 errors in context 2 of 2:
==3905== Invalid write of size 4
==3905==    at 0x401B17: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905==    by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905==    by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905==    by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905==  Address 0x51fce4c is 12 bytes inside a block of size 15 alloc'd
==3905==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3905==    by 0x401064: alloc_matrix (in /home/suraj/Desktop/project/fm)
==3905==    by 0x401A59: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905==    by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905==    by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905==    by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905== 
--3905-- 
--3905-- used_suppression:      2 dl-hack3-cond-1
==3905== 
==3905== ERROR SUMMARY: 14 errors from 2 contexts (suppressed: 2 from 2)

我从valgrind那里得到错误。

它说错误位于EliminateXr中,但我无法看到错误。 EliminateXr:

void EliminateXr(float** t,float* q,float*** tnew,float** qnew,int n1,int n2,int* r,int* s,int sprime){

    float** matrix = (float**)alloc_matrix(sprime,(*r)-1, sizeof(float));
    float* vec= (float*)malloc(sprime*sizeof(float));
    int matrixIndex=0;
    int i,k,l;
    for(k = 0; k < n1; ++k){
        for(l = n1; l < n2; ++l){
            for(i=0; i < *r; ++i){
                matrix[matrixIndex][i]=t[k][i]-t[l][i];
            }
            vec[matrixIndex]=q[k]-q[l];
            matrixIndex++;
        }
    }
    for(k = n2; k < *s; ++k){
        for(i=0; i < *r; ++i){
            matrix[matrixIndex][i]=t[k][i];
        }
        vec[matrixIndex]=q[k];
        matrixIndex++;
    }
    *tnew=matrix;
    *qnew=vec;  
    *r=(*r)-1;
    *s=sprime;

}

我正在为浮点数分配内存,所以我不应该写入大小为4的无效内容。 任何人都可以解释我应该如何使用这些信息: 地址0x51fc724是一个大小为39的块内的36字节 == 3905 ==在0x4C2A2DB:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)

如果我们查看我的alloc_matrix,我有一个malloc

s = m * n * block + m * sizeof(void*) + block - 1;
p = malloc(s);

block = float的大小。

根据valgrind的错误在哪里?

1 个答案:

答案 0 :(得分:5)

我的精神力量在这条线上说

float** matrix = (float**)alloc_matrix(sprime,(*r)-1, sizeof(float));

您正在分配一个能够容纳sprime行浮点数的矩阵,每行包含*r - 1列。

然而在你的内循环中

for(i=0; i < *r; ++i){
    matrix[matrixIndex][i]=t[k][i]-t[l][i];
}

您正在访问矩阵,就好像它包含*r列一样。 (i0转到*r - 1)请注意*r的值直到函数结尾后才会更改。

因此,你试图写过缓冲区的末尾,而Valgrind正当地抱怨。