2010-09-10 77 views
6

Valgrind显示大小为8的错误的未初始化值。 偶尔,下面的条件跳转对未初始化值的错误。C库中的Valgrind错误?

我所做的只是使用gcc 和内置的vsnprintf附带的stdC++库打印格式化的字符串。

这是一种称为format的方法,它是自定义字符串类的一部分。 现在是什么?一切看起来正确。错误似乎在_itoa.c里面。但是所有我能想到的在外面做的都不是使用这个功能,这是不太可能的!

==4229== Memcheck, a memory error detector 
==4229== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al. 
==4229== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info 
==4229== Command: ./test 
==4229== 
==4229== Use of uninitialised value of size 8 
==4229== at 0x54A3DF1: _itoa_word (_itoa.c:196) 
==4229== by 0x54A5138: vfprintf (vfprintf.c:1613) 
==4229== by 0x555C74F: __vsnprintf_chk (vsnprintf_chk.c:65) 
==4229== by 0x407E57: myString::format(char const*, ...) (stdio2.h:79) 
==4229== by 0x419D14: ID::toString() (id.cpp:151) 
==4229== by 0x41D03D: main (test.cpp:126) 
==4229== 
==4229== Conditional jump or move depends on uninitialised value(s) 
==4229== at 0x54A3DF8: _itoa_word (_itoa.c:196) 
==4229== by 0x54A5138: vfprintf (vfprintf.c:1613) 
==4229== by 0x555C74F: __vsnprintf_chk (vsnprintf_chk.c:65) 
==4229== by 0x407E57: myString::format(char const*, ...) (stdio2.h:79) 
==4229== by 0x419D14: ID::toString() (uuid.cpp:151) 
==4229== by 0x41D03D: main (test.cpp:126) 
==4229== 
==4229== 
==4229== HEAP SUMMARY: 
==4229==  in use at exit: 0 bytes in 0 blocks 
==4229== total heap usage: 6 allocs, 6 frees, 1,340 bytes allocated 
==4229== 
==4229== All heap blocks were freed -- no leaks are possible 
==4229== 
==4229== For counts of detected and suppressed errors, rerun with: -v 
==4229== Use --track-origins=yes to see where uninitialised values come from 
==4229== ERROR SUMMARY: 3 errors from 2 contexts (suppressed: 4 from 4) 

回答

5

这是在C图书馆,它实际上是为了格式化为一个字符串,看着你的电话号码的地方,它表示要格式化的数未初始化的存储来了。

有关未初始化值来源的更多详细信息,请添加valgrind选项--track-origins=yes

因为复制未初始化的内存很常见,例如填充结构中,valgrind跟踪未初始化值的复制,并且不会抱怨,直到实际使用值的位置可能会影响程序的外部可见行为。这可能会使混淆确定未初始化值的原始来源,因为在对其进行任何其他操作之前,它可能已被复制多次。选项--track-origins=yes跟踪附加信息以查明未初始化值的来源,以便在未初始化值结束使用时显示此信息。

1

如果它表示它在某个标准库中,则表示您传入的内容设置不正确。因此,为了进行调试,请转到层次结构中的第一行,即代码...如此:ID :: toString()(id.cpp:151)。

看看周围正在返回什么,你会发现你的罪魁祸首。