4

I am a C newbie and learning valgrind.

The following is my program. It compiles fine, but when I run valgrind, I see a "Conditional jump or move depends on uninitialised value(s)" stack trace.

I am trying to find where is the uninitialised value in the program. I am seeing a similar output when I use "--track-origins=yes" as well.

I tried looking at other questions on stack overflow, but could not find a definitive answer on this.

Where is uninitialised value?

Code:

  1 #include <stdio.h>
  2 
  3 int main()
  4 {
  5    int x = 5;
  6    x = 6;
  7    printf ("Hello World %d\n", x);
  8 
  9    return 0;
 10 }

Valgrind output is below.

==10154== Conditional jump or move depends on uninitialised value(s)
==10154==    at 0x1003FAC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==10154==    by 0x1001EEB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==10154==    by 0x1001F8FE5: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==10154==    by 0x10021E9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==10154==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==10154==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==10154==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==10154==    by 0x100000F5D: main (ex1.c:7)
==10154== 
Hello World 6
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
prgrmmr
  • 179
  • 1
  • 2
  • 12
  • 4
    You're obviously on a Mac. Which version of valgrind are you using, and what is your current OS ver ? I'm using 3.11.0 from macports valgrind-devel on Yosemite 10.10.3 and I don't see the issue you're describing. – WhozCraig May 31 '15 at 05:57
  • 1
    I cannot reproduce your problem. What's your exact code? What're your compiler options? What're your valgrind options? – dlask May 31 '15 at 05:58
  • 2
    Which version of [`valgrind`](http://valgrind.org/) and on which version of OS X, and with which compiler? Superficially, the problem is not in your code — it could be in the system library, though it isn't obvious how or why things are being misinterpreted. – Jonathan Leffler May 31 '15 at 05:58
  • The valgrind output also suggests that it's not your codes fault, which clearly isn't, there is no uninitialized value in your code, I have seen the same behavior previously with OS X, there are a couple of questions here on SO about the same issue. – Iharob Al Asimi May 31 '15 at 06:01
  • My valgrind version: valgrind-3.11.0.SVN and on Yosemite 10.10.3 as well! My compiler cmd: "$gcc -o ex1 ex1.c" and my valgrind cmd: "valgrind ./ex1". – prgrmmr May 31 '15 at 06:06
  • There is no problem with your program. It prints the correct value with no errors. It may be an issue with either valgrind or the stdio that you are using. Unfortunately, this is as far as I can help you. – Daniel Rudy May 31 '15 at 06:07
  • Thanks for all the comments, they clarified some of my questions. – prgrmmr May 31 '15 at 06:15
  • Are you using a GCC you built, one from MacPorts, the one from XCode (but which version of XCode), or what? I am on Yosemite 10.10.3, using GCC 5.1.0 which I built, and Valgrind 3.11.0.SVN from 2014-11-25 or thereabouts. That doesn't reproduce the problem. – Jonathan Leffler May 31 '15 at 06:20
  • @JonathanLeffler: Looks like I am using gcc (4.2.1) from Xcode - Version 6.3.2 (6D2105). – prgrmmr May 31 '15 at 06:30
  • That's clang in disguise...but I can't reproduce the problem with that, either. At least, not with: `/usr/bin/gcc --version` producing `Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.3.0 Thread model: posix`. And that comes from XCode - Version 6.3.2 (6D2105). – Jonathan Leffler May 31 '15 at 06:39
  • Strange. I get exactly similar output when I run `/usr/bin/gcc --version` except for `--prefix=/Applications/Xcode.app/Contents/Developer/usr` part. – prgrmmr May 31 '15 at 06:50
  • 1
    That probably doesn't matter, though it might. I'd not run xcode-select, so I was getting whatever it gives me. I've since run `sudo xcode-select -s /Applications/Xcode.app/Contents/Developer` and then I get the same `--prefix` as you (and I still don't get the error message from `valgrind`). On the whole, I wouldn't worry too much unless your programs are misbehaving. I have something like 17 suppressions recorded for valgrind, but I've not been using that suppressions file for testing your program. – Jonathan Leffler May 31 '15 at 06:58

1 Answers1

0

Valgrind might be indicating that the uninitialised value is used within a system library beyond the scope of your program, as evidenced by the number of times "(in /usr/lib/system/libsystem_c.dylib)" appears in the trace you've quoted

It might or might not be a Valgrind error. Valgrind has historically had some serious problems when running on OS X. There are other stable options! I've heard quite a bit of commotion about XCode Instruments, /usr/bin/leaks and /usr/bin/malloc_history, for example...

autistic
  • 1
  • 3
  • 35
  • 80