5

I'm trying to debug my code using valgrind. Most of the message I get are:

Conditional jump or move depends on uninitialised value(s) 

or

Invalid read of size 8 

I'm mainly concerned about the first, if the value was truly uninitialized I believe segmentation fault would occur. I tested this by sending the same pointer to another function along with uninitialized pointer to a function which I know throws a segmentation fault and only the truly uninitialized pointer has cause a segmentation fault.

What also might be the meaning of this error message.

Also, what does the second error means?

Edit1
Here is a model code, would that give error 1 (assume that the header files are legal)?

a.cpp

B b; C c; int main(){ return 0; } 

B.cpp

extern C c; // double t; //canceld, declared in the header. B::B(){ this->t = 1; c.test(t); } B::test(){ c.test(this->t); } 

B.cpp

C::C(){ } C::test(double t){ printf("%f\n",t); } 
11
  • 2
    Does valgrind point to specific lines of codes? It would be helpful to see some examples of the code that it flags. The first one may not necessarily have to do with pointers, but could be any variable that you don't initialize but then use in a conditional statement. Commented Aug 1, 2011 at 13:38
  • 2
    My experience with valgrind has been that "When it indicates an error and I can't see it" I'm just misreading the error and/or code and valgrind was actually right. Commented Aug 1, 2011 at 13:42
  • @unluddite, yes it does. Before I put this into my code, is it possible that this message is given because the function I use on the passed pointer actually belongs to a class which is initialized after I call the function? (I have put a model pseudo-code in my question) Commented Aug 1, 2011 at 13:48
  • @Mark B, this correspond to my edit and comment. My question is how to understand these errors. Commented Aug 1, 2011 at 13:57
  • @Yotam - If you call test() from B's constructor, that would be a problem. Otherwise it sounds more like you have an if (x > 5) where x doesn't have a value. That wouldn't segfault. Commented Aug 1, 2011 at 14:02

1 Answer 1

5
Conditional jump or move depends on uninitialised value(s) 

This means you are trying to do something to an uninitialized variable. For example:

int main() { int x; if (x == 5) printf("%d\n", x); return 0; } 

should do the trick. You can't compare/print or do something to an uninitialized variable.

Invalid read of size 8 

This means you are trying to read from memory that isn't there i.e. hasn't been allocated.

int main() { char* x = malloc(10); x[10] = '@'; //this is an invalid write printf("%c\n", x[10]); //this is an invalid read return 0; } 

Would cause an error because you've only allocated space for 10 characters, but you're writing/reading at the 11th character (remember, arrays are 0 indexed, so you can only write to 0-9).

"size X" in general is the amount of memory you're trying to read, so size 8 means you are trying to read 8 bytes.

Hope it helps. Post more specific code if you want debugging help. Valgrind generally tells you where the error occurs so you can figure out what to do.

Sign up to request clarification or add additional context in comments.

4 Comments

I this case we can guess that a double would 8 bytes.
Thanks, the problem is that in both cases the code does compile (not surprising) and run (surprising) through those errors, as far as I can tell the error is originated by the class that I'm using to run the function and not by the element I'm passing to the function. My actual segmentation fault is caused by completely different thing and I want to sort out the junk errors first.
@ Yotam - It probably is the element that you're passing. Oftentimes, valgrind will say the error is from function foo() in class bar, when it's actually the parameter you're passing to foo that's causing the error. If you could post the code, we could help you much more.
@BlackJack, I know that the element I'm passing is in valid (I have tested this).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.