Valgrind 无效写入 [英] Valgrind invalid write

查看:46
本文介绍了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 的错误.

I am getting errors from valgrind.

它说错误位于 EliminateXr 中,但我真的看不到错误.消除Xr:

It says that error is located in EliminateXr but i can't really see the error. 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 中)

I am allocating memory for float so i shouldn't get invalid write of size 4. Can anyone explain how i should use this information: 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)

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

If we look at my alloc_matrix i have a malloc

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

块=浮点数的大小.

valgrind 的错误在哪里?

Where are the error according to valgrind?

推荐答案

我的超能力说明了这一点

My psychic powers say that at this line

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

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

you are allocating a matrix capable of holding sprime rows of floats, each row containing *r - 1 columns.

然而在你这样的内循环中

Yet in your inner loop like this

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

您正在访问矩阵,就像它包含 *r 列一样.(i 是从 0*r - 1) 注意 *r 的值没有改变直到函数结束.

You are accessing the matrix as though it contains *r columns. (i is going from 0 to *r - 1) Note that the value of *r is not changed until later at the end of the function.

因此,您试图写越过缓冲区的末尾,Valgrind 理所当然地抱怨.

Hence, you are trying to write past the end of the buffer, and Valgrind rightfully complains.

这篇关于Valgrind 无效写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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