简体   繁体   English

为什么“艰难学习C”中的示例显示了valgrind中的错误?

[英]Why is an example from “Learn C the hard way” showing an error in valgrind?

I am trying to learn C and am reading about valgrind. 我正在尝试学习C,并且正在阅读有关valgrind的文章。 I have a simple C program, shown below, and when I run valgrind it complains about 我有一个简单的C程序,如下所示,当我运行valgrind时,它抱怨

Conditional jump or move depends on uninitialised value 有条件的跳跃或移动取决于未初始化的值

by 0x100000F50: main (ex5.c:9) 通过0x100000F50:主要(ex5.c:9)
==4338== Uninitialised value was created by a stack allocation == 4338 ==未初始化的值是由堆栈分配创建的
==4338== at 0x1001F5BF8: __vfprintf (in /usr/lib/system/libsystem_c.dylib) == 4338 ==在0x1001F5BF8:__vfprintf(在/usr/lib/system/libsystem_c.dylib中)

#include <stdio.h>

/* This is a comment. */
int main(int argc, char *argv[])
{
    int distance = 100;

    // this is also a comment
    printf("You are %d miles away.\n", distance);

    return 0;
}

Where is the error coming from? 错误从哪里来?

It is common for some implementations of library functions, in order to be most efficient, to skirt some of the rules that valgrind will check. 为了最有效地执行库函数的某些实现,通常会跳过valgrind将检查的某些规则。

Notice that the error is in __vfprintf (in /usr/lib/system/libsystem_c.dylib) . 请注意,错误位于__vfprintf (in /usr/lib/system/libsystem_c.dylib)

This is not always an issue. 这并不总是一个问题。 If there are no side effects (even libraries can have bugs) I would ignore it. 如果没有副作用(即使库也可能有错误),我将忽略它。

Valgrind often suppresses innocuous library warnings; Valgrind经常禁止无害的图书馆警告。 upgrading might include this one. 升级可能包括这一过程。

I don't see any issue in this code. 我在此代码中看不到任何问题。 It's fine. 没关系。 Check for the integrity of installed valgrind itself. 检查已安装的valgrind本身的完整性。 At times, valgrind may issue warnings (false positives, as we say), from the code in liked libraries, which are not part of your code and mostly out of your control and beyond your concern. 有时, valgrind可能会从喜欢的库中的代码发出警告(如误报),这不是您的代码的一部分,而且大多超出您的控制范围并且超出您的关注范围。

Try checking the code with some other (updated) version of valgrind . 尝试使用valgrind其他(更新)版本检查代码。

BTW, if you don't plan to use command-line argument, you can reduce main() to 顺便说一句,如果您不打算使用命令行参数,则可以将main()减少为

int main(void)
{  ....

Your code tested with valgrind 3.10 does not complain about anything. 使用valgrind 3.10测试的代码不会抱怨任何事情。 In many cases valgrind upgrade will solve the problem (via package manager or if not available then building yourself should be easy too). 在许多情况下,valgrind升级将解决问题(通过程序包管理器,或者如果不可用,则构建自己也应该很容易)。 I built the code using gcc 4.9.4. 我使用gcc 4.9.4构建了代码。

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

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