1

I have a problem with this bit of my code :

unsigned long me_hash(MEntry *me, unsigned long size){ 
    unsigned long hashval=0; 
    int i=0;
    for(i=0; me->surname[i];i++) hashval +=me->surname[i] + 28 * hashval;; 

    for(i=0; me->surname[i];i++){
      if(me->postcode[i]) { 
        hashval += me->postcode[i] + 28 * hashval;
      }
    }
    hashval += me->house_number;
    return (hashval%size);
}

The message which I get when I run Valgrind is

==4480== Conditional jump or move depends on uninitialised value(s)
==4480==    at 0x8048EB7: me_hash (mentry.c:66)
==4480==    by 0x8048B3E: ml_lookup (mlist.c:91)
==4480==    by 0x80488D2: main (finddupl.c:43)
==4480== 

I don't know how to fix this. Could you help me?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
user2013804
  • 29
  • 1
  • 4

1 Answers1

4

Conditional jump or move depends on uninitialised value(s)

This is really great information from Valgrind.

It basically says that the if or for conditions you have are depending on uninitialised value(s). Every time for example the for loop's condition is true, the execution jumps back at the start of it's body.

In other words, the message means that you are accessing memory that hasn't been initialized, as said in this answer.

These values are me->surname[i] or me->postcode[i]. Print them before use to see if they really have some garbage value (I guess they will).


Use the valgrind option --track-origins=yes to have it track the origin of uninitialized values. This will make it slower and take more memory, but can be very helpful if you need to track down the origin of an uninitialized value.

as suggested in this answer.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305