Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 让valgrind显示常见错误_C_Debugging_Valgrind - Fatal编程技术网

C 让valgrind显示常见错误

C 让valgrind显示常见错误,c,debugging,valgrind,C,Debugging,Valgrind,我使用--track origins=yes运行valgrind,因此我假设它会显示与未初始化变量相关的错误,如下所示: valgrind—轨迹原点=是。/pointer 也可以在各种文件中永久编辑valgrind的设置吗 我运行它的代码如下: #include <stdio.h> int main(){ int age = 10; int height; printf("I am %d years old.\n"); printf("I am %d inch

我使用--track origins=yes运行valgrind,因此我假设它会显示与未初始化变量相关的错误,如下所示: valgrind—轨迹原点=是。/pointer 也可以在各种文件中永久编辑valgrind的设置吗

我运行它的代码如下:

#include <stdio.h>
int main(){
   int age = 10;
   int height;
   printf("I am %d years old.\n");
   printf("I am %d inches tall.\n");

return 0;

}
我期待着这样的事情(从“艰苦学习c语言”中)


一般来说,我认为不能保证Valgrind会检测到这样的东西。如果获取但未传递的vararg来自堆栈,那么堆栈的该部分可能只是“意外”初始化的

然而,在这个特定的例子中,您可能看到了AMD64 ABI的效果,而您引用的示例似乎是x86(从地址不大于32位判断)。由于AMD64 ABI在寄存器中最多传递六个参数,
printf
甚至不会接触内存来获取参数,除非您给出更多参数

例如,我尝试了这个程序:

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%i %i %i %i %i %i %i\n");
    return(0);
}
#包括
int main(int argc,字符**argv)
{
printf(“%i%i%i%i%i%i%i%i\n”);
返回(0);
}
只有六个
%i
规格,Valgrind不会发出任何警告,而有七个规格,我会收到警告。我相当肯定这是因为参数是在寄存器中传递的


(然而,我承认,考虑到格式字符串本身是如何消耗一个寄存器的,我认为6
%I
规范应该已经足够了。我不能完全解释这种效果。除非,正如我上面提到的,第一个非寄存器参数恰好已在内存中初始化。)我忍不住注意到printf调用实际上并没有使用变量。你注意到了吗


这可能是它不起作用的原因。

我试过了,对我来说似乎也一样,谢谢你的帮助。我更改了这行:printf(“我有%d英寸高。\n”);->printf(“我有%d英寸高。\n”,高度);这就是你的意思吗?我还认为Dolda2000在某种程度上确实起了作用,当我试图像他那样用printf打印7个变量时
==3082== Memcheck, a memory error detector
==3082== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==3082== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==3082== Command: ./ex4
==3082== 
I am -16775432 years old.
==3082== Use of uninitialised value of size 8
==3082==    at 0x4E730EB: _itoa_word (_itoa.c:195)
==3082==    by 0x4E743D8: vfprintf (vfprintf.c:1613)
==3082==    by 0x4E7E6F9: printf (printf.c:35)
==3082==    by 0x40052B: main (ex4.c:11)
==3082== 
==3082== Conditional jump or move depends on uninitialised value(s)
==3082==    at 0x4E730F5: _itoa_word (_itoa.c:195)
==3082==    by 0x4E743D8: vfprintf (vfprintf.c:1613)
==3082==    by 0x4E7E6F9: printf (printf.c:35)
==3082==    by 0x40052B: main (ex4.c:11)
==3082== 
==3082== Conditional jump or move depends on uninitialised value(s)
==3082==    at 0x4E7633B: vfprintf (vfprintf.c:1613)
==3082==    by 0x4E7E6F9: printf (printf.c:35)
==3082==    by 0x40052B: main (ex4.c:11)
==3082== 
==3082== Conditional jump or move depends on uninitialised value(s)
==3082==    at 0x4E744C6: vfprintf (vfprintf.c:1613)
==3082==    by 0x4E7E6F9: printf (printf.c:35)
==3082==    by 0x40052B: main (ex4.c:11)
==3082== 
I am 0 inches tall.
==3082== 
==3082== HEAP SUMMARY:
==3082==     in use at exit: 0 bytes in 0 blocks
==3082==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3082== 
==3082== All heap blocks were freed -- no leaks are possible
==3082== 
==3082== For counts of detected and suppressed errors, rerun with: -v
==3082== Use --track-origins=yes to see where uninitialised values come from
==3082== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 4 from 4)
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%i %i %i %i %i %i %i\n");
    return(0);
}