0

I'm trying to implement the load function in speller, and receiving a segmentation fault. I have been careful to malloc all my pointers, so can't figure out what the problem is?? GDB says that the problem occurs in the line directly under the comment: "//add to beginning of linked list". Any ideas what could be going wrong? Thanks!

[code removed to comply to academic honesty]

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

bool load(const char* dictionary)
{
    // open dictionary file
    FILE* dict = fopen(dictionary, "r");

    for (int i = 0; i < 26; i++)
    {
        hashtable[i] = malloc(sizeof(node));
        hashtable[i] = NULL;
    }
}

[code removed to comply to academic honesty]

1

Your problem is that you correctly allocate memory for each of your nodes, but then you go and change the pointer pointing to this space by replacing it with NULL. You are not initializing it this way. malloc() returns a pointer to the allocated memory. If you also want to initialize this memory man calloc.

You must log in to answer this question.

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