0

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.

2
  • info on running speller with command line gdb here – DinoCoderSaurus Aug 9 '16 at 19:31
  • in that case it only merits comment status. If 'c' is apostrophe, then 'c' - 97 is.....? On the same note, is 26 "children" slots enough? – DinoCoderSaurus Aug 10 '16 at 0:00
0

The problem doesn't appear to be related to your clever code, although I haven't done any analysis of that part. ;-) It never gets that far. Based on what's posted above, here's where your code appears to blow up, in load() :

for(int i = 0; i < 26; i++)
root->children[i] = NULL;

Since it's not posted, I'm assuming that root is declared as a pointer as follows:

node * root;

Do you think it might have been useful to malloc some memory to root before trying to initialize any of it's elements?

Of course, if it's done somewhere else, then that would have been useful to know, but I don't think there's anywhere it could have been done before load.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .