User3
User3

Reputation: 2535

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

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?

Upvotes: 1

Views: 127

Answers (1)

Alex Reynolds
Alex Reynolds

Reputation: 96927

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.

The by 0xXYZ entries are memory addresses of caller functions, at each step of the stack backtrace. This is probably less useful to you than the line number and error message.

Repeat this parsing step for other errors.

Upvotes: 4

Related Questions