Halp,
I'm trying to implement a clever and zippy recursive trie makin function for load() in pset5, but my program's exploding when I trie, and I'm struggling to educe the problem (probably an extension of my ignorance since, as I can't get gdb working with speller at all, I may be a little dim).
Said function's declaration is:
void travis (node* trav, int c, FILE* dict);
Sparing a few tweaks to my header that should be obvious from context, here's what I've got:
bool load(const char* dictionary)
{
//opening the dictionary and error checking the file pointer
FILE* dict = fopen(dictionary, "r");
if(dict == NULL)
return false;
//just initializing some variables/nulling root fields, mom
int c = 0;
NUMWORDS = 0;
for(int i = 0; i < 26; i++)
root->children[i] = NULL;
while (c != EOF)
{
c = fgetc(dict);
travis (root, c, dict);
}
fclose(dict);
return true;
}
and now comes that allegedly clever, seg-faulting part that I thought for sure would compile and work on the first pass, instead of frustrating me for hours with the surety that I'm missing something very obvious from a conceptual dither :
void travis (node* trav, int c, FILE* dict)
{
//base case
if(c == '\n')
{
trav->isWord = true;
NUMWORDS++;
return;
}
//tentacle sprouting case
if(trav->children[c-97] == NULL)
{
//initialization!
node* tentacle = calloc(1, sizeof(node));
//insertion!
trav->children[c-97] = tentacle;
//inception!
c = fgetc(dict);
travis(trav, c, dict);
}
//tentacle elongating case
else
{
trav = trav->children[c-97];
c = fgetc(dict);
travis(trav, c, dict);
}
//case closed
return;
}
...but what, exactly, is that dither? My best guesses are in fgetc not properly evaluating EOF because of the function call, or maybe in using the child nodes of trav to move trav further along the trie. On that count, I can't think of why it wouldn't work, but something about it strikes me as wrong in an intestinal way.