c - 了解并尝试使用 Valgrind 进行 C 编程。

标签 c

我正在学习教程 From Here

这是我要运行的程序:

#include<stdio.h>

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

printf("The age is %d \n"); 
printf("The height is %d \n", height);

return 0; 

}

请注意错误是 self 引起的以检查功能

然后从命令行: $制作ex3 vlagrind ./ex3

我得到输出:

==6771== Memcheck, a memory error detector
==6771== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6771== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6771== Command: ./ex3
==6771== 
The age is -16776936 
==6771== Conditional jump or move depends on uninitialised value(s)
==6771==    at 0x4E7C4F1: vfprintf (vfprintf.c:1629)
==6771==    by 0x4E858D8: printf (printf.c:35)
==6771==    by 0x40052B: main (ex3.c:7)
==6771== 
==6771== Use of uninitialised value of size 8
==6771==    at 0x4E7A7EB: _itoa_word (_itoa.c:195)
==6771==    by 0x4E7C837: vfprintf (vfprintf.c:1629)
==6771==    by 0x4E858D8: printf (printf.c:35)
==6771==    by 0x40052B: main (ex3.c:7)
==6771== 
==6771== Conditional jump or move depends on uninitialised value(s)
==6771==    at 0x4E7A7F5: _itoa_word (_itoa.c:195)
==6771==    by 0x4E7C837: vfprintf (vfprintf.c:1629)
==6771==    by 0x4E858D8: printf (printf.c:35)
==6771==    by 0x40052B: main (ex3.c:7)
==6771== 
The height is 0 
==6771== 
==6771== HEAP SUMMARY:
==6771==     in use at exit: 0 bytes in 0 blocks
==6771==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6771== 
==6771== All heap blocks were freed -- no leaks are possible
==6771== 
==6771== For counts of detected and suppressed errors, rerun with: -v
==6771== Use --track-origins=yes to see where uninitialised values come from
==6771== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)
junaid@pc-dev-a147:~/Desktop/C folder$ valgrind --track-origins=yes ./ex3
==6788== Memcheck, a memory error detector
==6788== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6788== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6788== Command: ./ex3
==6788== 
The age is -16776936 
==6788== Conditional jump or move depends on uninitialised value(s)
==6788==    at 0x4E7C4F1: vfprintf (vfprintf.c:1629)
==6788==    by 0x4E858D8: printf (printf.c:35)
==6788==    by 0x40052B: main (ex3.c:7)
==6788==  Uninitialised value was created by a stack allocation
==6788==    at 0x4004F4: main (ex3.c:2)
==6788== 
==6788== Use of uninitialised value of size 8
==6788==    at 0x4E7A7EB: _itoa_word (_itoa.c:195)
==6788==    by 0x4E7C837: vfprintf (vfprintf.c:1629)
==6788==    by 0x4E858D8: printf (printf.c:35)
==6788==    by 0x40052B: main (ex3.c:7)
==6788==  Uninitialised value was created by a stack allocation
==6788==    at 0x4004F4: main (ex3.c:2)
==6788== 
==6788== Conditional jump or move depends on uninitialised value(s)
==6788==    at 0x4E7A7F5: _itoa_word (_itoa.c:195)
==6788==    by 0x4E7C837: vfprintf (vfprintf.c:1629)
==6788==    by 0x4E858D8: printf (printf.c:35)
==6788==    by 0x40052B: main (ex3.c:7)
==6788==  Uninitialised value was created by a stack allocation
==6788==    at 0x4004F4: main (ex3.c:2)
==6788== 
The height is 0 
==6788== 
==6788== HEAP SUMMARY:
==6788==     in use at exit: 0 bytes in 0 blocks
==6788==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6788== 
==6788== All heap blocks were freed -- no leaks are possible
==6788== 
==6788== For counts of detected and suppressed errors, rerun with: -v
==6788== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)

我无法从中得到任何结果,而正如教程告诉我的那样,我将获得发生错误的行号。如何读取 Valgrind 输出?

最佳答案

看看这里的最后一行:

The age is -16776936 
==6771== Conditional jump or move depends on uninitialised value(s)
==6771==    at 0x4E7C4F1: vfprintf (vfprintf.c:1629)
==6771==    by 0x4E858D8: printf (printf.c:35)
==6771==    by 0x40052B: main (ex3.c:7)

这是告诉您要查看的函数 (main)、文件 (ex3.c) 和行号 (7) .

by 0xXYZ 条目是调用函数的内存地址,在堆栈回溯的每一步。这对您来说可能不如行号和错误消息有用。

对其他错误重复此解析步骤。

关于c - 了解并尝试使用 Valgrind 进行 C 编程。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602842/

相关文章:

c - 将 C 指针插入堆栈,获取表

c++ - 需要帮助使用 GTK2 为 wxWidgets (wxGTK) 编写 "backgroundless"小部件

c - 将 atexit 注册到嵌套函数会在 gcc 下返回段错误

c - 关于中断?

字符数组和段错误

c - 如何翻转和反转 C 中的 int?

c - 带 float 的变量在 C 中保持为零

c - c char** 数组赋值的奇怪行为

c - 纹理管理/指针问题

c - 从 UART 接收的数据中获取子字符串并放入 LCD