未初始化的值是由堆栈分配创建的

时间:2021-09-06 22:02:31
==13890== Conditional jump or move depends on uninitialised value(s)
==13890==    at 0x4E7E4F1: vfprintf (vfprintf.c:1629)
==13890==    by 0x4E878D8: printf (printf.c:35)
==13890==    by 0x400729: main (001.c:30)
==13890==  Uninitialised value was created by a stack allocation
==13890==    at 0x400617: main (001.c:11)

The line being referenced:

线被引用:

int limit = atoi(argv[1]);

I am not sure how to fix it. I have tried searching on * and google but I could not find the solution.

我不知道怎么修复它。我已经尝试过在*和谷歌上搜索,但是我找不到解决方案。

Edit: This is the link to the full code.

编辑:这是完整代码的链接。

1 个解决方案

#1


10  

Have you checked argc and the contents of argv[1]? Is argv[1] guaranteed to be non-NULL in order to be suitable as input for atoi? Is it possible that atoi might be returning a trap representation representing an uninitialised value, due to argv[1] being non-numeric?

你检查过argc和argv[1]的内容了吗?argv[1]是否保证为非空,以便适合作为atoi的输入?由于argv[1]不是数值,atoi是否可能返回一个表示未初始化值的陷阱表示?

edit: After seeing the complete code, I've realised that that's not the problem, and your diagnosis is incorrect. Your problem is here: for (i = 0; i <= count; i++) { sum += numbers[i]; } Wheni == count, numbers[i] is uninitialised. This is because count is incremented after the last assignment to numbers[count] in the previous loop: numbers[count] = i; count++;. Hence, printing sum results in your message because sum itself depends upon an uninitialised value. Perhaps you meant for (i = 0; i < count; i++) { sum += numbers[i]; }

编辑:在看到完整的代码后,我意识到这不是问题所在,你的诊断是错误的。问题在这里:for (i = 0;我< =计数;{sum += number [i];}当i = count时,数字[i]未被初始化。这是因为在上一个循环中对number [count]的最后一次赋值之后,count会增加:numbers[count] = i;+ +,计数。因此,在您的消息中打印sum结果,因为sum本身依赖于一个未初始化的值。也许你的意思是(i = 0;我 <数;{sum +="number" [i];}< p>

#1


10  

Have you checked argc and the contents of argv[1]? Is argv[1] guaranteed to be non-NULL in order to be suitable as input for atoi? Is it possible that atoi might be returning a trap representation representing an uninitialised value, due to argv[1] being non-numeric?

你检查过argc和argv[1]的内容了吗?argv[1]是否保证为非空,以便适合作为atoi的输入?由于argv[1]不是数值,atoi是否可能返回一个表示未初始化值的陷阱表示?

edit: After seeing the complete code, I've realised that that's not the problem, and your diagnosis is incorrect. Your problem is here: for (i = 0; i <= count; i++) { sum += numbers[i]; } Wheni == count, numbers[i] is uninitialised. This is because count is incremented after the last assignment to numbers[count] in the previous loop: numbers[count] = i; count++;. Hence, printing sum results in your message because sum itself depends upon an uninitialised value. Perhaps you meant for (i = 0; i < count; i++) { sum += numbers[i]; }

编辑:在看到完整的代码后,我意识到这不是问题所在,你的诊断是错误的。问题在这里:for (i = 0;我< =计数;{sum += number [i];}当i = count时,数字[i]未被初始化。这是因为在上一个循环中对number [count]的最后一次赋值之后,count会增加:numbers[count] = i;+ +,计数。因此,在您的消息中打印sum结果,因为sum本身依赖于一个未初始化的值。也许你的意思是(i = 0;我 <数;{sum +="number" [i];}< p>