0

我已经阅读了几个关于这个 Valgrind 错误的线程,其中大多数都给出了本地化的响应。我想确切地知道出了什么问题,以便我以后可以自己调试这些错误。

我只会发布相关代码,如果有人想要更多,我也会发布..

所以我Invalid read of size 4在这条线上有一个:

int t = (p->acts+p->ac)->time;

请注意我认为可能与错误有关的指针算术?

我在这一行得到同样的错误,访问同一个成员:

next->time = next->acts[next->ac].time;

我该如何调试这个..程序运行良好,但我想知道发生了什么。

如果您需要更多信息,请发表评论。

4

1 回答 1

1

That error would indicate that next->ac (p->ac) is a value past the end of the memory allocated to next->acts (p->acts)

i.e...

next->acts = malloc( sizeof( something ) * count );
next->ac = count;

next>acts[next->ac].time = 0;

This would throw the error because count as an array index is actually one past the size of the array (base zero and all that)

Put another way, next->ac >= count would throw that error in the example I give.

Your program may work correctly because accessing past the end of allocated memory is undefined behavior. It could work, or it could spontaneously result in who knows what mayhem. But, all the same, accessing past the end of the allocated memory is an error.

于 2013-04-26T03:07:24.700 回答