1

I think it has been asked before, but I cannot find a proper solution. Speller works. Check50 gives green light, but in valgrind I get an error while unloading. It states that there are conditional jumps based on uninitiliazed values. I posted, the parts of code I think might be incorrect

typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
} node;

node* hashtable [TABLESIZE];

..// part of code
..// reading from dictionary

   node* new_node = malloc(sizeof(node));
   fscanf(dictionaryptr, "%s", new_node->word);

..//hashing and putting in hashtable
..//unload

for (int i =0; i < TABLESIZE; i++)
{
node* cursor = hashtable[i];
while (cursor!=NULL)
{
    node* temp = cursor;
    cursor = cursor->next;

    free(temp);
}

Can anybody help, this would be the final puzzle peace that is missing

Addendum: The output in valgrind states

==Conditional jump or move depends on uninitialised value(s) 
at 0X804A2FE: unload (dictionary.c:845)

That is the line:

node* cursor = hashtable [i]

using track--origins:

Uninitialised value was created by a heap allocation 
at ... Malloc
by ... dictionary.c:415
..//which is the line where the while lope starts where:
node* = new_node = malloc(sizeof(node));
fscanf(dictionaryptr, "%s", new_node->word);
3
  • Can you edit the question and add the output from valgrind ? More specifics may be helpful. Also, are there any other malloc or free commands in your code? – Cliff B May 4 '15 at 21:26
  • i don't see one conditional jump in the code example.if you use malloc to allocate memory that memory must be initialized before use, otherwise valgrind will flag it and it will not get past the leader boards testing – ebobtron May 5 '15 at 4:06
  • @CliffB I've added the valgrind output. only other malloc is the malloc I've used for (woord2) see other thread. That is freed properly now. – Mihaly May 5 '15 at 17:07
1

I suppose you never initialized every element in hashtable yet you're initializing cursor with hashtable[i] and checking, in the while loop continuation condition, whether cursor equals NULL.

Well, if you've never initialized hashtable[i] to NULL and you set cursor to hashtable[i], how can you rely on this logic without being sure what the value in cursor (or hashtable[i]) is?


Edit:

when you read a word from the dictionary, you allocate memory for a new node, you read the word into it, and eventually you insert it at the beginning of a linked-list.

But think about the very first node that you insert in a linked-list (or which eventually becomes the last node if you insert more nodes in that linked-list. What do you think the value of the next node of that node is? Well, you never really initialized it and that's what's causing the valgrind error. Looking at your while loop below

while (cursor != NULL)
{
    // some code
    cursor = cursor->next;
    // some code
}

eventually cursor will be equal to the last node in the list (that is not NULL), and in the next iteration, it should be equal to the next node to that node (which you assume is NULL), but you never really initialized it to NULL.

The fix is a bit obvious now I think. Let me know if you want more hints!

4
  • I actually tried initializing hashtable[i] to NULL by the following code in the load function for (int j=0; j<TABLESIZE; j++) { hashtable[j] = NULL; } However, the conditional jump remains... – Mihaly May 5 '15 at 16:54
  • @Mihaly you may email me with your dictionary.c and dictionary.h to take a deeper look! – kzidane May 5 '15 at 20:32
  • @Mihaly please read the Edit section in my answer! – kzidane May 6 '15 at 12:32
  • Indeed the fix was in initializing the first node. It's clearly not only the computer that needs to keep track of nodes. It's important for the programmer also. Now it works, without any bug. (bit slow, though). – Mihaly May 7 '15 at 18:59

You must log in to answer this question.

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