1

this is my code so far for pset5. I would appreciate giving me hints about my erros. I also get a segmentation fault. thanks.

when I implemented load and check only, it seemed to load the text and stop, didn't know if that meant the check function didn't work. then after implementing size and unload, I get segmentation fault, of the text still loads.

This is my dictionary.c file:

    /****************************************************************************
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 ***************************************************************************/

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "dictionary.h"

node* hashtable[26] = {0};
int count = 0;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{  
    int n = strlen(word);
    int found = 0;
    char wword[LENGTH + 1];
    node* new_cursor = NULL;

    for (int i = 0; i < n; i++)
    {
        wword[i] = tolower(word[i]);
    } 

    wword[n] = '\0';

    int position = (tolower(wword[0]) - 'a') % 26;

    if (hashtable[position] == NULL)
    return false;

    else
    {
        new_cursor = hashtable[position];
        while (new_cursor->next != NULL)
        {
            if (strcmp (new_cursor->word, wword) == 0)
            {              
               found++;
               return true;
            }

            new_cursor = new_cursor->next;
        }
    }

    if (strcmp (new_cursor->word, wword) != 0)
    return false;

    else
    return true;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{    
    FILE* fp = fopen(dictionary, "r");
    if (fp == NULL)
    {
        printf("Couldn't open dictionary\n");
        return false;
    }
    node* new_node = NULL;

    while(!feof(fp))
    {
        new_node = malloc(sizeof(node));
        fscanf(fp, "%s", new_node->word);
        int index = 0;
        index = (tolower(new_node->word[0]) - 'a') % 26;

        if ((hashtable[index]) == NULL)
        {
            hashtable[index]->next = new_node;
            count++;
        }

        else
        {
            new_node->next = hashtable[index];
            hashtable[index] = new_node;
            count++;
        }
    }
    fclose(fp);
    return true;     
}

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

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void)
{
    for (int i = 0; i < 26; i++)
    {
        node* freenode = NULL;
        node* tmp = NULL;
        freenode = hashtable[i];
        while (freenode != NULL)
        {
            tmp = freenode;
            freenode = freenode->next;
            free(tmp);
        }
        free(freenode);
    }
    return true;
}

1 Answer 1

1

use tools like gdb and valgrind to detect the cause of the problem and solve it!

your load function:

  • also do you think relying on feof may cause you to allocate more memory than you need?
  • are you sure the member named word in your struct has enough space to store the current word?
  • just else is enough as oppose to else if (hashtable[index] != NULL). see why?
  • regardless of the doubt about feof, the loop would break if and only if feof returned true in which case there wouldn't be a need for checking if feof(fp) again in a separate if statement after the loop.

your unload function:

  • why are you returning false?
21
  • thanks for the reply. I need to understand a couple of things. Can I scan words directly from the dictionary to the new_node while loading the dictionary? or do I need to create an array to copy the word first, where in this case I can make the while condition the fscanf (i couldn't do it in this case since I couldn't refer to new_node->word before mallocing the node). also, in case a words starts with an apostrophe, do I need to hardcode the result in this case?
    – Riham Nour
    Nov 1, 2015 at 11:41
  • @RihamNour of course you can read directly into your word member! also do you think there is a better way other than having to malloc memory for word separately? regarding apostrophes, maybe! :)
    – kzidane
    Nov 1, 2015 at 11:52
  • can a word possibly start with an apostrophe?
    – Riham Nour
    Nov 1, 2015 at 11:53
  • Oh I don't think so! you should probably ignore that point. it's more crucial when dealing with tries as opposed to hashtables. but also you don't want your array to be 27-element long, do you?
    – kzidane
    Nov 1, 2015 at 11:57
  • yea in that case, that's the point! It doesn't need to be 27-elements long! :)
    – Riham Nour
    Nov 1, 2015 at 12:00

You must log in to answer this question.

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