0

I have a somewhat okay understanding on how linked lists worked since I've had some experience working with them in high school. However, I keep running into these pesky segfaults and I have no idea why. Some help would be appreciated. Also let me know if there are any logical errors I might have overlooked. Thanks.

bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file==NULL) //if file could not be opened
    {
        fclose(file);
        return false;
    }
    char word[LENGTH]; //initializing string

    for (int i = 0; i < 1000; i++)
    {
        table[i] = NULL;
    }

    while (fscanf(file, "%s", word) != EOF)        //copying each word from the dictionary file to the word
    {

        node *n = malloc(sizeof(node)); //allocating memory for a new node
        if(n==NULL) //unable to allocate memory
        {
            unload();
            return false;
        }
        strcpy(n->word, word); //stores the string into the location given as the first argument
        unsigned long h = hash(word) % N;
        if(table[h] == NULL) //if nothing is linked to the array yet
        {
            table[h]= n; //setting the first link
            n->next=NULL;
            Number_Of_Words++;
            return true;
        }
        else if (table[h] != NULL) //if it is linked to something else already
        {

            n->next = table[h];
            table[h] = n;
            Number_Of_Words++;
            return true;
        }
    }

    return false;
}

Results:-

  • WORDS MISSPELLED: 17314
  • WORDS IN DICTIONARY: 1
  • WORDS IN TEXT: 17756
  • TIME IN load: 0.00
  • TIME IN check: 0.01
  • TIME IN size: 0.00
  • TIME IN unload: 0.00
  • TIME IN TOTAL: 0.01

[Re-edited code, I think load is the culprit here]

0

Disclaimer: I only quickly scanned the code, but these jumped out at me as common problems. I may be wrong.

You have checks in the code like these:

while (cursor->next!=NULL) //traversing the list

if(table[h]->next == NULL)

What happens if cursor == NULL or table[h] == null? The ->next element doesn't exist. That always generates a seg fault.

Say that there are no elements in the table[h] linked list. It will still be null.

In check, what happens at the end of the linked list? cursor->next will be null but the code won't check the last word in the list.

There could be other issues, but this jumped out at me. Look for other errors where the code is trying to access something that doesn't exist.

Hint: does the code replace the correct elements when inserting new words into the beginning of the linked list?

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

2
  • Hey, I tried messing around with the conditions and got rid of the seg faults. Unfortunately though, my code is still messed up since it doesn't copy more than one word from the dictionary. WORDS MISSPELLED: 17314 WORDS IN DICTIONARY: 1 – Sorphil Apr 1 '20 at 7:56
  • Not totally unexpected. But that would be a new problem with new code that requires a new question. This one was only to address the seg faults. ;-) Please accept this answer, if you feel it solved the seg fault issue and open a new question. thanks. – Cliff B Apr 5 '20 at 18:03

You must log in to answer this question.

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