解释valgrind错误无效的写入大小4

时间:2012-05-07 10:57:30

标签: gcc valgrind

我最近试图追踪我正在使用valgrind的程序中的一些错误,我得到的错误之一是:

==6866== Invalid write of size 4
==6866==    at 0x40C9E2: superneuron::read(_IO_FILE*) (superneuron.cc:414)

违规行#414读取

amplitudes__[points_read] = 0x0;

和幅度__早先定义为

uint32_t * amplitudes__ = (uint32_t* ) amplitudes;

现在显然uint32_t是4个字节长,所以这是写入大小,但是有人可以告诉我它为什么无效吗?

2 个答案:

答案 0 :(得分:6)

points_read很可能超出范围,您正在为amplitudes分配的内存过去(或之前)。

答案 1 :(得分:3)

新程序员做出此警告的典型错误是:

struct a *many_a;
many_a = malloc(sizeof *many_a * size + 1);

然后尝试在位置'尺寸'

中读取或写入内存
many_a[size] = ...;

这里的分配应该是:

many_a = malloc(sizeof *many_a * (size + 1));
相关问题