0

I don't know if this is a problem others have had?

Having some issues with the dictionary load function in pset4. I decided to implement a trie and thru following debug50, I can see that it is loading the words "a" and "aaa" into the data structure. However, it seems to hit a seg fault on the third character of the next word: "aaas"

No idea why this seems to be happening? I get the following output from valgrind:

Looks like you're trying to access 8 bytes of memory that isn't yours? Did you try to index into an array beyond its bounds? Take a closer look at line 79 of dictionary.c.

Note - line 79 is ... if (curr->children[charInt] == NULL)

    // Buffer for a word
char word[LENGTH + 1];
int charInt, wordLen;

node *curr = root;

for (int l = 0; l < N; l++)
{
  curr->children[l] = NULL;
}


// Insert words into trie
while (fscanf(file, "%s", word) != EOF)
{
    // TODO
    wordLen = strlen(word);

    curr = root;

    for (int i = 0; i < wordLen; i++)
    {
        if (word[i] == 39)
        {
            charInt = 26;  // the case for an apostrophe
        }
        else
        {
            charInt = word[i] - 97;     // gets letter into a format of a = 0, b = 1 etc
        }

        if (curr->children[charInt] == NULL)
        {
            curr->children[charInt] = malloc(sizeof(node));   // create new node if it does not exisit
            curr = curr->children[charInt];

            // need to set all of the children to null as it is a new path
            for (int j = 0; j < N; j++)
            {
                curr->children[j] = NULL;
            }

        }
        else
        {
            curr = curr->children[i];    // go to the next node if it exists
        }
    }

    curr->is_word = true;
}

// Close dictionary
fclose(file);

// Indicate success
return true;

}

You must log in to answer this question.

Browse other questions tagged .