0

I am trying to overcome the segmentation fault on the following code, and I couldn't manage to solve the error. Segmentation fault occurs in the load() function, and after hours of debugging, I couldn' find the way to solve. Any help would be appreciated.

// Implements a dictionary's functionality

#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>

#include "dictionary.h"

// Represents a node in a hash table == Kind of like a linked list.
typedef struct node
{
    char word[LENGTH + 1]; /* the word */
    struct node *next;
}
node;

int word_count = 0;
// Number of buckets in hash table
const unsigned int N = 20; /* O(n/N) - time complexity. */

// Hash table
node *table[N]; /* the global array containing linked lists to prevent collisions */

// hash function, check() function, size() function and unload() function... 

// the problem is with the load() function.

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    for (int i = 0; i < N; i++)
    {
        table[i] = NULL;
    }
    // TODO
    FILE *in = fopen(dictionary, "r");
    if (in == NULL)
    {
        return false;
    }
    /* read words one at a time */
    char word[LENGTH + 1];
    while (fscanf(in, "%s", word) != EOF)
    {
        node *w = malloc(sizeof(node));
        if (w == NULL)
         {
             printf("Hop on there, node is null.\n");
             return false;
         }
         for (int i = 0; word[i]; i++)
         {
             w->word[i] = word[i];
         }
         uint32_t index = hash(word); /* table[index] */
         if (table[index] == NULL)
         {
             /* there is no previous entry for the index */
             table[index] = w;
             w->next = NULL;
         }
         else
         {
             w->next = table[index]; /* get the head of the linked list, and assign it to the new_node */
             table[index] = w;
         }
         word_count++;
     }
    // Read strings from file one at a time
    
    printf("Code still working?\n"); /* nope, the program can't reach here. (segfault) */
    fclose(in);

    return true;
}

EDIT 1 I have tried https://cee.studio/ to debug my seg fault, but when I ran the same exact code there, the code worked. But in the cs50 ide, it still gives me seg error.

EDIT 2 Now, I have tried the same exact code on my local machine, and it works perfectly. But still cs50 ide gives me seg fault.

1

It looks like an intermittent problem that's going to rear it's ugly head depending on what's in memory and what was in memory before.

Since your whole code wasn't posted, I looked fairly closely at the load function. This jumps out at me:

     for (int i = 0; word[i]; i++)
     {
         w->word[i] = word[i];
     }

It looks like the code is trying to copy the word into the node, but it's flawed. It does copy the word, but not the end of string marker at the end. Since the node isn't initialized, it may lead to unpredictable results.

Why not just strcpy(w->word, word)?

But I'm not convinced that's where the problem lies. It would be easier to test with the full code.

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

1
  • Thanks for your answer, Cliff. Well, I tried using strcpy(), but that didn't resolve the issue. However, when I created a new directory inside the cs50 and copied the contents of the project into this directory, somehow the code worked as expected :)
    – DarthVader
    Jul 3 at 19:05

You must log in to answer this question.

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