Valgrind memcheck uses a bunch of heuristics to avoid false positives on "harmless" uses uninitialized values, since such uses are common both in correct and incorrect-but-otherwise functioning code.
In particular, it doesn't barf until you actually use such a value in a serious, perhaps "irreversible" way, e.g, jumping based on its value.
This means that sometimes the error occurs very far from the origin of the problem and it is not even possible to determine which value is involved. Is there some way to "check" a value at runtime, like use(x) which will make Valgrind emit an error at that spot if x is uninitialized?
Usually, stuff like this requires instrumentation of the code (either done automatically by a tool, or inserted manually in the source).
As noted in my comment, if you can work with having to insert the
use(x)statements yourself, you could do something like this:However there is a problem with using
structs that contain alignment-padding. The padding is never used for anything, so this can be a legal reason for passing around uninitialized data. Valgrind can cause spurious errors regarding uninitialized reads in this case exactly.These two posts discuss this issue specifically: Is there anyway a valgrind message "Conditional jump or move depends on uninitialized value" can be a so called 'false positive'
Should I worry about "Conditional jump or move depends on uninitialised value(s)"?