4

I just finish debugging my code for pset5 dictionary.c, It's working correctly, It passed check50 too.

Now the problem I am encountering is in valgrind my heap is all freed, But wherever in condition, I used

cur -> children[c]

cur -> is_word

it (valgrind) tells me "Conditional jump or move depends on uninitialised value(s)"

14

The problem is in the conditions right?

specifically in the node, pointed to by node* cur

Now it's a problem because the node, node* cur is pointing to is "uninitialized"

When we malloc something in the memory, it just allocate some portion of memory and give the address of the starting block of the memory allocated, to the pointer, and that's it.

Now if we are putting values in it that's fine, but if we are checking for values (NULL) without putting values (NULL) in there in the first place, creates problems.

Now to solve this problem you have to initialized the memory you are provided by the malloc before you check for the conditions.

To do this you can initialize using a for loop setting every element in the node to 0

node* new_node = (node*) malloc(sizeof(node));
for (int i = 0; i < 27; i++)
{
    new_node->children[i] = NULL;
}
//similar method can be used in case of Hash Table

But remember whole node need to be initialized and above for loop just initialize the children array in the node.

( In case of Tries) So now you can either initialize other elements separately ie is_word to false

Or, you can use memset() which can initialize all elements in the node for you, like this:

node* new_node = (node*) malloc(sizeof(node));
memset(new_node, 0, sizeof(node));

'0' both represent NULL and false, so whole node will be initialize as needed in case of dictionary.c.

And hopefully this will clear all the 9911860 something errors you are getting.

9
  • I tried to keep it as simple as possible :) – Vipin kumar Jun 14 '16 at 4:50
  • Finally fixed it.... Thanks~~ Just one question. Why did you add (node*) before malloc? – Eric Yang Aug 27 '17 at 7:02
  • @eric I used (node*) at the time because I want to explicitly state the use of memory allocated. But peoples opinions is split about explicitly stating this. Both works, but there is some difference that probably doesn't matter at this level. Read the two most upvoted answers here for more information about using and not using it. – Vipin kumar Aug 27 '17 at 12:49
  • Got it. Thanks~~ – Eric Yang Sep 1 '17 at 6:55
  • This was soooo helpful!!!! – Jacob1164 Jul 15 '18 at 22:44
10

I had a ton of errors like that, too...

But ALL these errors disappeared, when I changed only 4 single characters in my code!

Here's how:

I simply used calloc() in place of malloc()!
calloc() needs an additional argument for the number of blocks sizeof(x), so use calloc(1, sizeof(node)) instead of malloc(sizeof(node)). It's as simple as that!

So, what calloc() does is it not only allocates memory like malloc, but also initializes it to a universal zero value, that means: Every check for NULL or a character has a defined outcome. And as others have said before me, that's what valgrind wants to have!

Now you might be just like me and think "But that's gonna ruin my great time!". But fret not, you will be pleasantly surprised by how fast calloc() works! My numbers didn't change at all (at least not the 2 significant figures we get to see).

Hope that helps!

(PS: Sorry for the clickbait intro, I couldn't resist!)

4

You must log in to answer this question.

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