When my code runs I get a seg fault. I ran GDB and it crashes where you can see the ***. The strange thing is that in GDB I do not see any value in buffer before the crash, whereas it should have read a word from the dictionary. Really don't know what's up. Tips much appreciated, CB

 * Loads dictionary into memory.  Returns true if successful else false.
bool load(const char* dictionary)
    // open dictionary
    FILE* inptr = fopen(dictionary, "r");
    if (inptr == NULL)
        printf("Could not open %s.\n", dictionary);
        return false;

    // buffer
    char buffer[LENGHT + 1];

    // store each word in table
    while(fscanf(inptr, "%s", buffer) == 1)
        // make new node & insert read word
        node* new_node = malloc(sizeof(node));
        new_node->next = NULL;
        strcpy(new_node->word, buffer);

        // hash the word
        int key = hash_it(buffer);

        // if indexed location is empty then insert node there
        *** if(hashtable[key]->next == NULL)
            hashtable[key] = new_node;     

        // else insert node afer indexed location
            // set pointer of new node to the next node
            new_node->next = hashtable[key]->next;

            // set pointer of indexed location to new node
            hashtable[key]->next = new_node;

        // add 1 to dictionary counter
    return true;

1 Answer 1


// if indexed location is empty then insert node there if(hashtable[key]->next == NULL)

you're assuming that the new node points to a valid place in this case NULL, I think that's a wrong assumption. hashtable[key], is a pointer which should point to the new node created, I would eliminate your condition IF-ELSE, and only do:

new_node-> next = hashtable[key];

hashtable[key] = new_node;
  • That does the job. There are still some other problems in the code, just so readers know:)
    – CB Nuji
    May 30, 2016 at 6:32
  • you can send me your code if you need help
    – MARS
    May 30, 2016 at 18:13

You must log in to answer this question.

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