0

I rectified the earlier error in my code (putting dictionary within inverted commas). I also corrected my error in unload where the function would always return false instead of true. However, I couldn't identify any other error in my program that would lead to a segmentation fault. Could someone help me out, please?

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "dictionary.h"
#include <string.h>
#include <ctype.h>
#include <strings.h>

int count = 0;

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    char *term = NULL;
    strcpy(term, word);
    int array_location = hash(term);
    for(node *find = table[array_location]; find != NULL; find = find->next)
    {
        if (strcasecmp(word, find->word) == 0)
        {
            return true;
        }
    }
    return false;
}


// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    char first_letter = word[0];
    char capital_letter = toupper(first_letter);
    int letter_number = atoi(&capital_letter);
    int hash_code = letter_number - 65;
    return hash_code;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    for(int y = 0; y < 26; y++)
    {
        table[y] = NULL;
    }
    FILE *f = fopen(dictionary, "r");
    if (f != NULL)
    {
        char *term = NULL;
        while(fscanf(f, "%s", term) != EOF)
        {
            fscanf(f, "%s", term);
            node *nd = malloc(sizeof(node));
            if (nd != NULL)
            {
                strcpy(nd->word, term);
                int array_location = hash(term);
                if(table[array_location] == NULL)
                {
                    nd->next = NULL;
                    table[array_location] = nd;
                    count++;
                }
                else
                {
                    nd->next = table[array_location];
                    table[array_location] = nd;
                    count++;
                }
            }
            else
            {
                fclose(f);
                return false;
            }
        }
    }
    else if (f == NULL)
    {
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    for(int i = 0; i < 26; i++)
    {
        node *marker = table[i];
        node *deleter = marker;
        while (deleter != NULL)
        {
            marker = marker->next;
            free (deleter);
            deleter = marker;
        }
    }
    return true;
}
1

The seg fault lies here:

    char *term = NULL;
    while(fscanf(f, "%s", term) != EOF)
    {
        fscanf(f, "%s", term);
        node *nd = malloc(sizeof(node));
        if (nd != NULL)
        {
            strcpy(nd->word, term);

Note first that the pointer term is set to NULL. That means that no memory is allocated to that pointer. The fscanf() call will have unpredictable results, but term will still be set to null. I believe that it will try to assign the data that was read to NULL, ie, nowhere.

Finally, the strcpy will try to access the address stored in term, which will fail, generating a seg fault.

Instead of using a memory pointer for term, try a char array of the maximum word length plus 1 for the end of string marker.

There are more problems to follow, but one question, one problem. Also, you should have a chance to work on it.

Tip: have you read this? Do YOU know how to find a seg fault?? Advice to new programmers

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 .