1

When I run valgrind on my speller code I get errors that all look similar to this:

==4398== Conditional jump or move depends on uninitialised value(s)
==4398==    at 0x401260: load (dictionary.c:131)
==4398==    by 0x4008CD: main (speller.c:40)
==4398==  Uninitialised value was created by a heap allocation
==4398==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4398==    by 0x4011A5: load (dictionary.c:93)
==4398==    by 0x4008CD: main (speller.c:40)

Here is my struct:

typedef struct node
{
    bool isWord;
    struct node *children[27];
}
node;

Here is Zamyla's pseudocode that I followed closely:

for every dictionary word, iterate through trie
each element in children corresponds to a different letter
check the value at children[i]
    if NULLL, malloc a new node, have children [i] point to it
    if not NULL, move to new node and continue
if at end of word, set isWord to be true

And here is my dictionary.c code in the load function (I'm using a trie) from line 130. Comments are referring to the pseudocode:

// if NULL, malloc a new node, have chilren [i] point to it
if (cursor->children[i] == NULL)
{
cursor->children[i] = malloc(sizeof(node));
cursor = cursor->children[i];
}

// if not NULL, move to new node and continue
else
{
cursor = cursor->children[i];
}

Valgrind is freaking out about line 131 which is where I'm checking if children[i] is NULL:

if (cursor->children[i] == NULL)

How would I go about fixing this error?

1

Okay so through some research I managed to solve the problem by initialising all the elements when creating a node, by passing it to function. This person's question helped: Conditional jump or move depends on uninitialised value(s)

| improve this answer | |

You must log in to answer this question.

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