0

I have this code:

node *cursor = hashtable[i];
        while(cursor != NULL)
        {
            if (strcmp(word_cpy, cursor -> word) == 0)
            {
                return true;
            }

            cursor = cursor -> next;
        }
        free(cursor);

I have initialized every element in the hashtable to be NULL; when the program gets to this point in the code, some objects of hashtable is NULL while others have linked lists connected to them.

However, there is a problem with the while conditional statement: while(cursor != NULL), to which valgrind says "Conditional jump or move depends on uninitialised value(s)"

By encoding node *cursor = hashtable[i], don't I initialize the cursor? How do I get this valgrind error?

Thanks

  • The problem is most likely related to the earlier code that sets the array and its elements. Can you please add all the code thats related, including the node structure, the initialization of the nodes, etc. – Cliff B Jul 12 '18 at 21:01
  • paste.ofcode.org/FAvrV3kvSyyrEyYr8iDriL the lines that have the same valgrind error occurs anytime I have that while conditional, of which I use alot through out the code. – faelesterio Jul 13 '18 at 4:33
  • specific lines of the same and only error in this program is on: 76, 85, 163, and 171. thanks in advance. – faelesterio Jul 13 '18 at 4:38
1

Always do new_entry -> next = hashtable[hashtable_index];, even if the right side is NULL. Your code does not initialise new_entry -> next, this would fix that.

Also, you have a few free statements that are always passing NULL, namely on line 85, 114, 171. They are useless and can be removed.

| improve this answer | |
  • the errors for valgrind happen in 76 and 163 (where the while statement conditionals are). So the errors exists in other functions outside of function load(). Also, on line 136 I do end up making new_entry -> next = hashtable[hashtable_index] – faelesterio Jul 13 '18 at 18:47
  • I tried setting node *cursor = NULL, and then cursor -> next = hashtable[i] (for lines 76 and 163) but I just get segmentation fault – faelesterio Jul 13 '18 at 18:48
  • Thanks in advance @Blauelf, deleting those free statements helped too – faelesterio Jul 13 '18 at 18:48
  • Do not set cursor->next to anything. But do set new_entry->next in load, do not let it be uninitialised. Also, you should use the same hash function in both load and check, if you test everything you could have used a single linked list instead. – Blauelf Jul 13 '18 at 20:58
  • thanks for the help; but this still doesn't fix the valgrind errors show in this link: cs50.me/checks/8e08077ec117c60586ad2942dc27b0ca0eae6444 my new code with Blauelf's new changes are here: paste.ofcode.org/ZPmnApgAfEr9WPLPgwyF5S – faelesterio Jul 13 '18 at 23:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .