实例介绍利用valgrind定位变量未初始化的问题

    xiaoxiao2021-03-25  60

           继续介绍valgrind的用途, 看程序:

    #include <stdio.h> #include <string.h> int main() { int i; if(i == 0) { printf("[%d]\n", i); } return 0; }      一眼就能看出程序的问题, valgrind分析如下:

    [root@xxx ~/valgrind-3.8.1/bin]# g++ -g test.cpp [root@xxx ~/valgrind-3.8.1/bin]# [root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out ==8088== Memcheck, a memory error detector ==8088== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==8088== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==8088== Command: ./a.out ==8088== ==8088== Conditional jump or move depends on uninitialised value(s) ==8088== at 0x4005B0: main (test.cpp:7) ==8088== ==8088== Use of uninitialised value of size 8 ==8088== at 0x5612A9B: _itoa_word (in /lib64/libc-2.12.so) ==8088== by 0x5615652: vfprintf (in /lib64/libc-2.12.so) ==8088== by 0x561E189: printf (in /lib64/libc-2.12.so) ==8088== by 0x4005C5: main (test.cpp:9) ==8088== ==8088== Conditional jump or move depends on uninitialised value(s) ==8088== at 0x5612AA5: _itoa_word (in /lib64/libc-2.12.so) ==8088== by 0x5615652: vfprintf (in /lib64/libc-2.12.so) ==8088== by 0x561E189: printf (in /lib64/libc-2.12.so) ==8088== by 0x4005C5: main (test.cpp:9) ==8088== ==8088== Conditional jump or move depends on uninitialised value(s) ==8088== at 0x56140E3: vfprintf (in /lib64/libc-2.12.so) ==8088== by 0x561E189: printf (in /lib64/libc-2.12.so) ==8088== by 0x4005C5: main (test.cpp:9) ==8088== ==8088== Conditional jump or move depends on uninitialised value(s) ==8088== at 0x5614101: vfprintf (in /lib64/libc-2.12.so) ==8088== by 0x561E189: printf (in /lib64/libc-2.12.so) ==8088== by 0x4005C5: main (test.cpp:9) ==8088== [0] ==8088== ==8088== HEAP SUMMARY: ==8088== in use at exit: 0 bytes in 0 blocks ==8088== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==8088== ==8088== All heap blocks were freed -- no leaks are possible ==8088== ==8088== For counts of detected and suppressed errors, rerun with: -v ==8088== Use --track-origins=yes to see where uninitialised values come from ==8088== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 6 from 6) [root@xxx ~/valgrind-3.8.1/bin]#       问题显而易见。

    涛歌依旧 认证博客专家 排名第一 点链接学人工智能 公众号免费领资料 ❤️零基础入门进阶人工智能 ❤️欢迎关注涛哥公众号,免费领海量学习资料。涛哥:毕业后就职于华为和腾讯。微信:ai_taogeyijiu
    转载请注明原文地址: https://ju.6miu.com/read-36286.html

    最新回复(0)