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.