Why does valgrind reports on uninitialised bytes in the following code?
#include <valgrind/memcheck.h>
class Test {
public:
Test() {}
};
int main(int argc, char* argv[]) {
Test a;
VALGRIND_CHECK_VALUE_IS_DEFINED(a);
return 0;
}
If I add a member variable to test and initialise it, there is no output.
In your example, the size of ‘a’ will be 1:
sizeof()only reports the data in a class, but since each unique class must have it’s own address, a padding byte is added. The reason valgrind complains is that you are accessing the padding byte, which the compiler has no obligation to initialize to any value.See here for a comment on why the minimum size of a class is 1 and not 0.