4
votes

Valgrind has kept complaining about uninitialized bytes and by trimming down in look for a minimal example I have ended up with this:

#include <valgrind/memcheck.h>

struct dummyObject{
    int foo;
    bool bar;

    dummyObject():foo(1),bar(true) {}
};

int main(){

    dummyObject dummy;

    VALGRIND_CHECK_VALUE_IS_DEFINED(dummy); 

    return 0;
}

Having two ints or two bools, or a single int or bool, cause no complaints. It looks as if having a class with members of different types leads to Valgrind complaining. This is not simply due to my explicit request for checking either; in a larger program where object similar to dummyObject gets used, I 'm getting the "Conditional jump or move depends on uninitialised value(s)" error.

My compiler is g++ 4.7.3 on a 64-bit linux, compiling with debug flags and no optimizations - or with, it makes no difference.

Is there something I 'm missing, or is it a false positive?

1
It complains on padding bytes which cannot be initialized by compiler. - lapk
Same problem here with gcc 4.7.2 and Valgrind 3.9.0. I'm quite new of C++ but this seem to me an huge issue in Valgrind. Here is somebody having the same problem stackoverflow.com/questions/19364942 - Gab

1 Answers

2
votes

When you check the size of your type you will probably find that the size doesn't match the sum of the size of the members. For example, on my system I get:

sizeof(dummy)=8 sizeof(int)=4 sizeof(bool)=1

when printing the different sizes. The difference is padding used to make sure the objects are aligned to addresses where they can be easily accessed by the system. It is probably this padding which is uninitialized.