我在与Valgrind的一个问题: 这是我的节目(以及在错误出现的主要部分):Valgrind的:大小的无效读出的1
int main()
{
char comanda[N];
....
char *p;
while(fgets(comanda,100,stdin)!=NULL)
{
p=strtok(comanda," \n");
if (strcmp(comanda,"INIT")==0)
{
p=strtok(NULL," ");
Init(n);
}
395 >>if (strcmp(p,"DUMP")==0)
{
Dump(n);}
if (strcmp(p,"ALLOC")==0)
{
Alloc(j,n);
}
....return 0;}
当我跑Valgrind的,它说:
Invalid read of size 1 at 0x401569: main (:395) Address 0x0 is not stack'd malloc'd or (recently) free'd
我想不出有什么用此程序的问题。
在'strtok'之后检查'p'是否为NULL。 – DCoder
在调试器下运行程序以确定他们做错了什么也可能是一个好主意。 –
此处不需要调试器,valgrind正是在这里说明发生了什么。 @ user1934103,请阅读它告诉你的内容。在它指示的地方,什么地址可以是'0x0'?所以它告诉你'p'是'0'。 –