简体   繁体   English

为什么Valgrind抱怨printf而不是我的未初始化的计数器?

[英]Why is Valgrind complaining about printf instead of my uninitialized counter?

I just started using Valgrind, and I'm not sure the error messages I get are what they should be. 我刚刚开始使用Valgrind,我不确定我得到的错误消息是他们应该是什么。 For instance, Valgrind just gave me a very long chain of warnings about printf() making jumps based on uninitialized memory. 例如,Valgrind给了我一个很长的警告链,关于printf()根据未初始化的内存进行跳转。 It was obviously that the issue wasn't with printf() . 显然问题不在于printf() Rather, my program was giving printf() tainted memory. 相反,我的程序给了printf()污染的内存。

I managed to produce the following MCVE: 我设法生产了以下MCVE:

#include <stdio.h>

int main(void)
{
    int d;
    d++;
    printf("%d\n", d);
    return 0;
}

It's really obvious that the problem here lies in d++ . 很明显,这里的问题在于d++ However, Valgrind only detects and warns me of uninitialized memory usage in the next line, with 6 messages in the form 但是,Valgrind只检测并警告我下一行中未初始化的内存使用情况,表单中有6条消息

==12178== Conditional jump or move depends on uninitialised value(s) == 12178 ==条件跳转或移动取决于未初始化的值(s)
==12178== at 0x4E7F79D: vfprintf (vfprintf.c:1636) == 12178 ==在0x4E7F79D:vfprintf(vfprintf.c:1636)
==12178== by 0x4E871F8: printf (printf.c:33) == 12178 == by 0x4E871F8:printf(printf.c:33)
==12178== by 0x1086D1: main (mcve.c:7) == 12178 == by 0x1086D1:main(mcve.c:7)

I compiled this with 我编译了这个

gcc mcve.c -g -O0 gcc mcve.c -g -O0

And I ran Valgrind with 我和Valgrind跑了

valgrind --leak-check=yes ./a.out valgrind --leak-check = yes ./a.out

Then I discovered there's --track-origins=yes . 然后我发现有--track-origins=yes It attempts to help, but it gets lost easily with pointers. 它试图提供帮助,但它很容易被指针迷失。 For instance, it doesn't work for the next MCVE: 例如,它不适用于下一个MCVE:

#include <stdio.h>

int f2(int *p)
{
    (*p)++;
    return *p;
}

int f1(void)
{
    int d;
    return f2(&d);
}

int main(void)
{
    printf("%d\n", f1());
    return 0;
}

Here it says that the error is in the stack frame of f1() . 这里它说错误是在f1()的堆栈帧中。 It's kind of helpful, but considering the performance penalty involved, maybe it's not worth it. 它有点帮助,但考虑到所涉及的性能损失,也许它不值得。

What can I do to make best use of Valgrind? 我能做些什么来充分利用Valgrind?

A deliberate decision in Valgrind is that it does not complain just because some uninitialised memory is copied or similar. Valgrind的一个深思熟虑的决定是,它不会因为某些未初始化的内存被复制或类似抱怨。 It complaints only when the use of uninitialised memory changes the behaviour of your program. 只有当使用未初始化的内存改变程序的行为时,它才会抱怨。 This deliberate choice avoids a lot of false-positive errors. 这种刻意的选择避免了许多误报错误。

See http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.uninitvals for more information. 有关更多信息,请参阅http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.uninitvals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM