I have decided to use trie to implement load in speller and it works fine with the small dictionary (~14 words), however it seg faults with the large dictionary. Using valgrind, it says:
Invalid read of size 8
at 0x4012A0: load (dictionary.c:77)
by 0x400874: main (speller.c:40)
Address 0x51fdef8 is not stack'd, malloc'd or (recently) free'd
I have a hard time understanding the problem or even how to fix it. Any help would be great Here's my implementation for load:
bool load(const char *dictionary)
{
// TODO
root = calloc(1, sizeof(trie));
if (root == NULL)
{
printf("ERROR.\n");
return false;
}
trie *trav = root;
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
return false;
for(int c = fgetc(dict); c != EOF; c = fgetc(dict))
{
if(c != '\'' && c != '\n' && trav->letters[c%97] == NULL)
{
trav->letters[c % 97] = calloc(1, sizeof(trie));
trav = trav->letters[c % 97];
}
if(trav->letters[c % 97] != NULL)
trav = trav->letters[c % 97];
if (c == '\'')
{
trav->letters[26] = calloc(1, sizeof(trie));
trav = trav->letters[26];
}
if(c == '\n')
{
trav->isWord = true;
words++;
trav = root;
}
}
fclose(dict);
return true;
}