如何识别valgrind中未初始化的值?

时间:2012-01-05 01:48:31

标签: c valgrind

这可能看起来像一个愚蠢的问题,但valgrind默认情况下不会给你足够的信息。 Valgrind报告如下:

==2541== Conditional jump or move depends on uninitialised value(s)
==2541==    at 0x401777: process_read.clone.3 (in /home/matt/dev/ocs/client3/a.out)
==2541==    by 0x4026B8: obbs_main (in /home/matt/dev/ocs/client3/client)
==2541==    by 0x53D1D8B: start_thread (pthread_create.c:304)
==2541==    by 0x511D04C: clone (clone.S:112)

我看不出任何明显的东西。 Valgrind -v也没有帮助。

有没有办法让valgrind告诉我哪些值是未初始化的?

2 个答案:

答案 0 :(得分:4)

如果在valgrind中使用--track-origins=yes标志,它将告诉您行号(假设您使用-g编译)首先分配了单位化内存的位置。这通常是在某个函数开头的堆栈分配。

尝试使用-Wall进行编译。 -Wall应该在编译时捕获大多数“未使用的未初始化”错误。

答案 1 :(得分:1)

Valgrind通知您使用未初始化的值 - 而不仅仅是未初始化的值,例如:

==1029== Memcheck, a memory error detector
==1029== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==1029== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==1029== Command: a.out
==1029==
==1029== Conditional jump or move depends on uninitialised value(s)
==1029==    at 0x4004D7: main (uninit.c:6)
==1029==
==1029==
==1029== HEAP SUMMARY:
==1029==     in use at exit: 0 bytes in 0 blocks
==1029==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==1029==
==1029== All heap blocks were freed -- no leaks are possible
==1029==
==1029== For counts of detected and suppressed errors, rerun with: -v
==1029== Use --track-origins=yes to see where uninitialised values come from
==1029== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
[adrian@iceweasel ~]$ cat uninit.c
#include <stdio.h>
int main(int argc, char *argv[])
{
   int i;

   if(i)
   {
      printf("Hello\n");
   }
   return 0;
}