gpt4 book ai didi

c - Valgrind 无效写入

转载 作者:太空宇宙 更新时间:2023-11-04 05:57:41 24 4
gpt4 key购买 nike

==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 中,但我看不到错误。消除Xr:

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;

}

我正在为 float 分配内存,所以我不应该得到大小为 4 的无效写入。谁能解释我应该如何使用这些信息:地址 0x51fc724 是分配大小为 39 的 block 内的 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,错误在哪里?

最佳答案

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

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

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

然而在你的内循环中是这样的

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

您正在访问矩阵,就像它包含 *r 列一样。 (i0*r - 1)注意 *r 的值没有改变直到稍后在函数结束时。

因此,您试图写入超过缓冲区的末尾,而 Valgrind 理所当然地提示。

关于c - Valgrind 无效写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24217519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com