【问题标题】:Valgrind won't detect obvious errorsValgrind 不会检测到明显的错误
【发布时间】:2013-06-14 16:13:21
【问题描述】:

我正在向 Zed Shaw 的优秀Learn C the Hard Way学习 C。

我正在运行 Debian Sid,内核版本为 3.9.0-vanillaice amd64(我编译它)和 glibc 2.17(从 Debian 的 repo 安装)。

我在Exercise 4 in the book

这是我的代码:

#include <stdio.h>

//This program is purposefully messed up.

int main()
{
        int age = 10;
        int height;

        printf("I am %d years old.\n");
        printf("I am %d inches tall.\n", height);

        return 0;
}

Valgrind 应该会产生这样的结果:

$ valgrind ./ex4
==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)
$

我没有提及错误:

$ valgrind ./ex4
==4098== Memcheck, a memory error detector
==4098== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==4098== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==4098== Command: ./ex4
==4098== 
I am -16776056 years old.
I am 0 inches tall.
==4098== 
==4098== HEAP SUMMARY:
==4098==     in use at exit: 0 bytes in 0 blocks
==4098==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4098== 
==4098== All heap blocks were freed -- no leaks are possible
==4098== 
==4098== For counts of detected and suppressed errors, rerun with: -v
==4098== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

【问题讨论】:

    标签: linux debian valgrind glibc memcheck


    【解决方案1】:

    我假设您使用的是 gcc 编译器。

    我只是做了一些测试,只有当我使用 -O0 构建程序时,我的 valgrind 才报告错误。对 (-O3) 进行优化后,valgrind 没有报告错误。我想那是因为编译器能够删除未初始化的跳转,因为该值永远不会改变并被完全删除。由于您这样做是为了练习,您可能会尝试找出哪些编译器选项适合您。如果您还不知道那是什么,请查找“Debug”或“Release”构建并切换到“Debug”。

    HTH, 驼鹿

    【讨论】:

    • 现在尝试...编辑:有效!我之前是在优化级别为 2 的情况下实现的。
    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 2013-03-28
    • 2016-02-03
    • 1970-01-01
    • 2021-08-19
    • 2015-07-02
    • 1970-01-01
    相关资源
    最近更新 更多