0

I posted this to the CS50 Subreddit with no help, so i thought i'd post here and try to get some help. My load function is not working, and keeps producing a segmentation error at (hashtable[index]->next = new_node;) and i've tried everything to figure out what is wrong with it to no avail. Any help would be very much appreciated!

(I'm also struggling to figure out how / when to return true, so any tips regarding that would also be great)

bool load(const char *dictionary)
{
    typedef struct node
    {
        char word[LENGTH + 1];
        struct node *next;
    }
    node;

    node *hashtable[26];
    node *last;
    char word[LENGTH + 1];
    bool letterused = false;
    int charusednow = 0;
    int charusedbefore = 0;
  //  node *head = NULL;
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        fprintf(stderr, "The Dictionary does not Exist");
        return false;
    }

    while (fscanf(dict, "%s", word) != EOF)
    {
        charusednow = word[0];
        if (charusednow != charusedbefore)
        {
            letterused = false;
        }

        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            unload();
            return false;
        }
        strcpy(new_node->word, word);
        int index = word[0] % 26;
        if (letterused == false)
        {
            hashtable[index]->next = new_node;
            letterused = true;
        }
        else
        {
            last->next = hashtable[index]->next;
            while (last->next != NULL)
                last = last->next;
            last->next = new_node;
        }
        charusedbefore = word[0];
    }

    return true;
}
1

The code is generating a seg fault because it is trying to access a nonexistent element in a structure.

This line of code, node *hashtable[26];, creates the hashtable array, but doesn't initialize it. Pointers are not initialized to any default value or NULL when created. They simply contain whatever garbage data is contained in memory at that location instead of an address for a specific memory location. Since your code doesn't initialize the hashtable array or allocate any memory to it, the entire array contains random, garbage data. The next thing done with the hashtable array is this:

        hashtable[index]->next = new_node;

Since no memory was allocated to hashtable[index], the ->next element doesn't exist. Attempting to store something in it causes a seg fault. Instead, you could store the address of the new node directly in the array.

        hashtable[index]  = new_node;

As a separate side note, the code places a new node at the end of the linked list. While this works, it is inefficient. A more efficient method would be to insert the new node at the beginning of the list.

Also, what would happen to your code if the dictionary wasn't a sorted alphabetical list? Something to think about. ;-)

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

1
  • thank you so much Cliff, much appreciated! Dec 12 '17 at 22:52

You must log in to answer this question.

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