Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting valgrind error

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);
}
like image 254
Yotam Avatar asked Aug 01 '11 13:08

Yotam


1 Answers

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.

like image 62
BlackJack Avatar answered Sep 23 '22 20:09

BlackJack