Skip to main content
3 of 3
Commonmark migration

Can't even get GDB to run on my speller file

I won't even say how long I have been working on pset5, its embarrassing. But I have now at least got code written which I am sure is going to have a lot of mistakes. Problem is I can't even run GDB on the speller file to debug. I keep getting the following error message

(gdb) run ~cs50/pset5/texts/austinpowers.txt Starting program: /home/ubuntu/workspace/pset5/speller >~cs50/pset5/texts/austinpowers.txt

Program received signal SIGSEGV, Segmentation fault. 0x000000000040142f in load (dictionary=0x401622 "dictionaries/large") at >dictionary.c:115 115 hashtable[hashy(new_node->word)] = new_node; (gdb)

This is my load function that contains the offending line (115). Can anyone give some guidance as to what the error might be so I can at least get to a point where I can run GDB. Many Thanks.

bool load(const char* dictionary)
{
//initialise hashtable to NULL
for (int i = 0; i < 27; i++)
{
    hashtable[i] = NULL;
}

// open dictionary file and read it
FILE* dic = fopen(dictionary, "r");

//if empty return false
if (dic == NULL)
{
    printf("Failed to load dictionary.\n");
    return false;
}
else 
{
    //continue until end of file reached
    while(!feof(dic))
    {
        
        //build a new node 
        node* new_node = malloc(sizeof(node));
        
        //read in the words from the dictionary
        fscanf(dic, "%s", new_node -> word);
        count++;
        
        
        
        //put word through hash function to get index for the array
        index = hashy(new_node-> word);
        
        //if first word at index, put it in and point to Null
        if (hashtable[hashy(new_node-> word)] == NULL)
        {
            hashtable[hashy(new_node->word)] = new_node;
            new_node->next = NULL;
        }            
        //if there is already a word at the index, add the new word
        else
        {
            new_node->next = hashtable[hashy(new_node->word)];
           hashtable[hashy(new_node->word)] = new_node;
            
        }
    }