I have been working on this error for couple days, but still cannot figure out what the problem is. The program compiles and finds the misspelled words in the beginning, but after printing out about 3/4 of the misspelled words it suddenly stopped working. There are probably errors in my unload function. I haven't really worked on the unload function because every time I run valgrind it gives me this problem, but in case it is related, I also upload the code of unload function. I used trie here. Can anyone please take a look at my code and give me some advice? Thank you in advance!
#include <stdbool.h>
#include <stdlib.h>
#include "dictionary.h"
#include <string.h>
#include <ctype.h>
#include <stdio.h>
typedef struct node
{
bool is_word;
struct node * children[27];
}
node;
node *root;
int count;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
node *trav = root;
for (int i = 0; i < strlen(word); i++)
{
int indexarray;
if (isalpha(word[i]))
{
indexarray = tolower(word[i]) - 'a';
}
else
{
indexarray = 26;
}
trav = trav->children[indexarray];
if (!trav)
{
return false;
}
if (i == strlen(word) - 1)
{
if (trav->is_word)
{
return true;
}
}
}
return false;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
count = 0;
FILE *dictionaryfile = fopen(dictionary, "r");
if (!dictionaryfile)
{
return false;
}
char word[LENGTH + 1];
root = calloc(1, sizeof(node));
node *trav = root;
if (!root)
{
return false;
}
while (fscanf(dictionaryfile, "%s", word) != EOF)
{
for (int i = 0; i < strlen(word); i++)
{
int indexarray;
if (isalpha(word[i]))
{
indexarray = word[i] - 'a';
}
else
{
indexarray = 26;
}
if (!trav->children[indexarray])
{
trav->children[indexarray] = calloc(1, sizeof(node));
if (!trav->children[indexarray])
{
return false;
}
}
trav = trav->children[indexarray];
if (i == strlen(word) - 1)
{
trav->is_word = true;
count++;
}
}
trav = root;
}
fclose(dictionaryfile);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return count;
}
bool freetrie(node *ptr);
bool checknull(node *ptr);
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
node *cursor = root;
if (freetrie(cursor))
{
free(cursor);
return true;
}
return false;
}
bool freetrie(node *ptr)
{
node *trav = ptr;
while (ptr)
{
for (int i = 0; i < 27; i++)
{
ptr = trav->children[i];
if (ptr)
{
if (checknull(ptr))
{
free(ptr);
}
else
{
freetrie(ptr);
}
}
}
}
return true;
}
// check if all children are null
bool checknull(node *ptr)
{
while (ptr)
{
int j = 0;
while (j < 27)
{
if (!ptr->children[j])
{
j++;
}
else
{
return false;
}
}
}
return true;
}
Here is how it looks like when I compare my answer to the solution. When I run check50, it shows that it could not find the misspelled words.