简体   繁体   English

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

[英]Understanding and trying to use Valgrind for C programming.

I am following a tutorial From Here 我正在从这里学习教程

This is the program I am trying to run: 这是我要运行的程序:

#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; 

}

Please note the errors are self induced to check on functionality

Then from the command line: $ make ex3 vlagrind ./ex3 然后从命令行:$ make ex3 vlagrind ./ex3

I get the output: 我得到的输出:

==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)

I am not able to make anything out of it, while as the tutorial tells me that I will get line numbers at which the error has occurred. 我无法从中获得任何收益,而正如本教程告诉我的那样,我将获得发生错误的行号。 How to read the Valgrind output? 如何读取Valgrind输出?

Take a look at the last line here: 在这里看最后一行:

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)

This is telling you the function ( main ), the file ( ex3.c ) and the line number ( 7 ) to review. 这是在告诉您要检查的功能( main ),文件( ex3.c )和行号( 7 )。

The by 0xXYZ entries are memory addresses of caller functions, at each step of the stack backtrace. by 0xXYZ条目是调用程序函数的内存地址,在堆栈回溯的每个步骤中。 This is probably less useful to you than the line number and error message. 这可能对您没有行号和错误消息有用。

Repeat this parsing step for other errors. 重复此分析步骤以解决其他错误。

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

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