hookenz
hookenz

Reputation: 38967

How do I identify which values values are uninitialised in valgrind?

This might seem like a silly question but valgrind doesn't by default give you quite enough information. Valgrind reports the following:

==2541== Conditional jump or move depends on uninitialised value(s)
==2541==    at 0x401777: process_read.clone.3 (in /home/matt/dev/ocs/client3/a.out)
==2541==    by 0x4026B8: obbs_main (in /home/matt/dev/ocs/client3/client)
==2541==    by 0x53D1D8B: start_thread (pthread_create.c:304)
==2541==    by 0x511D04C: clone (clone.S:112)

I can't see anything obvious. Valgrind -v also doesn't help.

Is there a way to get valgrind to tell me which values are uninitialsed?

Upvotes: 2

Views: 1281

Answers (2)

Adrian Cornish
Adrian Cornish

Reputation: 23896

Valgrind notifies you about use of uninitialized values - not just uninitialized values eg:

==1029== Memcheck, a memory error detector
==1029== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==1029== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==1029== Command: a.out
==1029==
==1029== Conditional jump or move depends on uninitialised value(s)
==1029==    at 0x4004D7: main (uninit.c:6)
==1029==
==1029==
==1029== HEAP SUMMARY:
==1029==     in use at exit: 0 bytes in 0 blocks
==1029==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==1029==
==1029== All heap blocks were freed -- no leaks are possible
==1029==
==1029== For counts of detected and suppressed errors, rerun with: -v
==1029== Use --track-origins=yes to see where uninitialised values come from
==1029== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
[adrian@iceweasel ~]$ cat uninit.c
#include <stdio.h>
int main(int argc, char *argv[])
{
   int i;

   if(i)
   {
      printf("Hello\n");
   }
   return 0;
}

Upvotes: 1

Paul
Paul

Reputation: 141897

If you use the --track-origins=yes flag with valgrind it will tell you the line number (assuming you compiled with -g) where the unitialized memory was first allocated. This is usually at the stack allocation at the beginning of a function somewhere.

Try compiling with -Wall as well. -Wall should catch most "used uninitialized" errors at compile time.

Upvotes: 4

Related Questions