0

I have been trying to resolve errors in my speller "load function". I have been able to resolve a couple of them except this one. Debug50 reveals that while creating a link between new Node and the the linked list, "Process received SIGSEGV: Segmentation fault" is received. I dry run the code and made sketches. My logic seems to be correct but for some reason I am unable to implement it correctly. Hope someone will help.

dictionary.h:

// Declares a dictionary's functionality

#ifndef DICTIONARY_H
#define DICTIONARY_H

#include <stdbool.h>

// Maximum length for a word
// (e.g., pneumonoultramicroscopicsilicovolcanoconiosis)

#define LENGTH 45
#define TABLE_SIZE 100

typedef struct node
{
    char word[LENGTH+1];
    struct node *next;
}
node;


// Prototypes
bool check(const char *word);
bool load(const char *dictionary);
unsigned int size(void);
bool unload(void);
int getASCII(const char *word);
int hashIndex(int ascii);
void initialize(node *hashTable[]);

#endif // DICTIONARY_H

dictionary.c is as below:

// Implements a dictionary's functionality

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <cs50.h>
#include "dictionary.h"

// HashTable of size TABLE_SIZE is created (Default: 100)
node *hashTable[TABLE_SIZE];

//Number of words loaded
int wordCount = 0;

//Check dictionary loaded or not
bool loaded = false;

int getASCII(const char *word)
{
    //Gets ASCII sum of the word
    int ascii = 0;
    int length=strlen(word);
    for(int i = 0; i < length; i++)
    {
        ascii += word[i];
    }
    return ascii;
}

int getHashIndex(int ascii)
{
    return ascii % TABLE_SIZE;
}


// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //Gets the hash value of word
    int index = getHashIndex(getASCII(word));
    bool flag = false;
    node *temp = hashTable[index];
    do
    {
        if(strcmp(temp -> word, word) == 0)
        {
            flag = true;
            return flag;
        }
        temp = temp -> next;
    }
    while(temp != NULL);
    return false;



}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    //Initialise
    for(int i = 0; i < TABLE_SIZE; i++)
    {
        hashTable[i] = NULL;
    }

    //Open file
    FILE* infile = fopen(dictionary,"r");

    if(infile)
    {
       int hashIndex = 0;
       char word[LENGTH+1];

       // Iterate over the dictionary
       while(fscanf(infile, "%s", word) != EOF)
       {
            // Place \0 at the end of word
            for(int i = 0; i < (LENGTH + 1); i++)
            {
                if((word[i] < 65 && word[i] != 39) || (word[i] >= 90 && word[i] <= 96) || (word[i] >= 123 && word[i] <= 127))
                {
                    word[i] = '\0';
                    break;
                }
            }

            // Calculates hash value for the word
            hashIndex = getHashIndex(getASCII(word));

            // Reserves space for the new node
            node *newNode = malloc(sizeof(node));

            // Exit if memory is full
            if(newNode == NULL)
            {
                unload();
                return false;
            }

            //Increment words in dictionary
            wordCount ++;

            //Copies the word into new node
            strcpy(newNode -> word, word);

            // Create link
            // TODE: Segmentation fault at next line
            newNode -> next = hashTable[hashIndex];
            hashTable[hashIndex] = newNode;
            loaded = true;
       }
       return loaded;
    }
    return false;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    if(loaded)
    {
        return wordCount;    
    }
    else
    {
        return 0;
    }

}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    bool flag = false;
    for(int i = 0; i < TABLE_SIZE; i++)
    {
        node *cursor = hashTable[i];
        while(cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor -> next;
            free(temp);
            flag = true;
        }
    }
    return flag;
}
4
  • Please edit and add the struct definition code for node.
    – Cliff B
    Jul 28, 2018 at 7:54
  • @CliffB I have added the struct definition. Can you help me out?
    – Raffay
    Jul 28, 2018 at 9:33
  • Better yet, can you post the full code? Too many missing pieces here.
    – Cliff B
    Jul 28, 2018 at 17:44
  • @CliffB So now I have included the complete code for the header and c files. Hope you can help me know. Please note that rest of the .c files are same as provided by the pset.
    – Raffay
    Jul 29, 2018 at 6:29

1 Answer 1

0

Never generated a seg fault, but found a couple of problems that should be easy to find.

Here's the big hint. Have you noticed that the vast majority of words that are identified as misspelled all have capital letters in them?????

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 .