I'm trying to use a trie structure to implement the load function in speller. I get a segmentation fault with the large dictionary, but not the small one. Text file size does not seem to matter, (I have not implemented check or unload yet). I've looked over some of the other questions asked about this issue, but none of the answers seem to apply to mine. Here is my code. It fails on else if (trav->children[asc] == NULL)
during the large dictionary load, but I'm not sure why.
Here's the code in question:
bool load(const char *dictionary)
{
// make space for root
root = malloc(sizeof(node));
//traversal pointer
node *trav = root;
//integer for ascii conversion
int asc = 0;
//open dictionary
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
fprintf(stderr, "Dictionary %s\n is NULL\n", dictionary);
return false;
}
for(int a = fgetc(dict); a != EOF; a = fgetc(dict))
{
asc = (int)a % 97;
// if newline char, then set is_word to true, start over at root for next word
if(asc == 10)
{
trav->is_word = true;
trav = root;
}
// if the pointer is not pointing to anything (other than NULL), point to appropriate child[index]
else if(trav->children[asc] == NULL)
{
// create a new node, then point to that node...
trav->children[asc] = malloc(sizeof(node));
trav = trav->children[asc];
}
// if an apostrophe, put at end of node array
else if(asc == 39)
{
trav = trav->children[26];
}
else
{
trav = trav->children[asc];
}
}
return true;
}