0

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;
            
        }
    }

1 Answer 1

0

There are a couple of possibilities. You could be trying to access a nonexistent array element in hashtable[]. This could happen if hashy(new_node-> word) is returning a number that's too big or negative. (Haven't seen your hash function.) This also comes into play in another way, as you're about to see.

More likely though, I would bet that you're seeing a side effect of the way you're dealing with EOF on your dictionary. You are reading from the dictionary and, unfortunately, processing what was read BEFORE you check for and process the EOF condition. Remember that you can read right up to the EOF without it being detected. In fact, that's exactly what happens. The last word is read, but the EOF isn't detected until the following read (i.e., the EOF isn't detected when the last word is read even though it is the very next thing in the file following the last word.) The result is that you're processing past the end of the dictionary file. How's your hash function going to handle that? If you weren't hitting the seg fault, the EOF wouldn't be handled until you completed the pass through the loop and came back up to the top. Your word count is probably off by 1 too many as well.

You need to determine whether the dictionary is being fully read, or if the seg fault occurs before the EOF to see if there are other issues.

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

You must log in to answer this question.

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