My program works fine with the small dictionary. But when I attempt to load the large library it gets a segmentation fault when loading the 1908th dictionary word. The code that I am using is below. I've isolated the problem at the node* new_node = malloc(sizeof(node)) statement. I'm at wits end trying to understand what is the problem.

/** * Loads dictionary into memory. Returns true if successful else false. */

bool load(const char* dictionary) {

char word[LENGTH + 1] = "                                              ";

//initialize hash table
char blankWord[LENGTH + 1] = "                                              ";

for (int i = 0; i < 500; i++)

    hashTable[i] = malloc(sizeof(node));
    strcpy(hashTable[i]->word, blankWord);
    hashTable[i]->next = NULL;

// open dictionary as input
FILE* infile = fopen(dictionary, "r");

// if no file opened, return an error.
if (infile == NULL)
    return false;

// until end of dictionary entries
while(fscanf(infile, "%s", word) != EOF)
    int hashKey = getHashKey(word);
    node* new_node = malloc(sizeof(node));

    strcpy(new_node->word, word);

    if(hashTable[hashKey]->next == NULL)

        new_node->next = NULL;           
        hashTable[hashKey]->next = new_node;        

        new_node->next = hashTable[hashKey]->next;           
        hashTable[hashKey]->next = new_node;

Your code loads the small dictionary which only has a few words, but your dictionary, as written, will not be efficiently searchable, and will almost certainly contain memory leaks. Here are some questions to think about:

  1. why bother initializing word and blankword as " " when you can just declare them?

  2. how is getHashKey() function defined? I assume it assigns a word to the right node of your hashtable based on its first letter. If so, why do you have i<500 when there are only 26 letters in the alphabet?

  3. related to #2 above, review the lecture to see how hash table nodes are initialized

  • 1.)I'm using blankWord to intialize my hashtable->word variables. When I set it to NULL I immediately got a segmentation fault. 2.) My hashtable has 500 buckets. I perform an arithmetic operation on the characters and then mod 500 the result 3.) I've tried to initialize the table several ways. The way I have it now it at least loads the first 1907 words from the large dictionary. I'm not sure why it gets a segmentation fault after loading the 1908th word. valgrind didn't find any memory leaks. – Vinnis Lee Bryant Feb 14 '15 at 6:29

Finally after two days. After allocating the new node, instead of initializing the .word variable, I let it default. I still initialized the .next variable with NULL. This seems to have resolved my issue. Whew! Wiping sweat from brow.

You must log in to answer this question.

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