5

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)"

2 Answers 2

19

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.

10
  • I tried to keep it as simple as possible :) Commented Jun 14, 2016 at 4:50
  • Finally fixed it.... Thanks~~ Just one question. Why did you add (node*) before malloc?
    – Eric Yang
    Commented Aug 27, 2017 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. Commented Aug 27, 2017 at 12:49
  • Got it. Thanks~~
    – Eric Yang
    Commented Sep 1, 2017 at 6:55
  • This was soooo helpful!!!!
    – Jacob1164
    Commented Jul 15, 2018 at 22:44
13

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!)

8
  • 1
    DUDE YOU ARE A GOD Commented Apr 7, 2020 at 4:25
  • 1
    I second his statement Commented May 31, 2020 at 5:44
  • 1
    THANK YOU SO MUCH Commented Aug 13, 2020 at 12:58
  • 1
    thank you asdhjkf
    – Hannah Meg
    Commented Sep 16, 2020 at 17:54
  • 1
    You actually made my night :D had been struggling with my assignment until 3 am and my last issue was exactly this and the calloc just fixed it immediately. You. Are. A. Legend!
    – Achi Roy
    Commented Jul 23, 2021 at 0:26

You must log in to answer this question.

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