0

i get this error from check50 and i can t figure out why...:

:( handles max length (45-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

everything else is green (well except for the memory errors but that is unrelated)

*my hash function just returns the number of chars in the word (i know it is not fast but i just want to understand the basics first and i can modify it later on into something more time efficient)

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

// 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 = LENGTH;

// Hash table
node *table[N - 1];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //hash the word in the text
    int h = hash(word);
    
    //ignore words longer than 45 chars
    if (h > N)
    {
        return true;
    }
    
    //create a list location following current node 
    node *current[N - 1];
    current[h] = table[h];
    
    //look up word in the dictionary at the hash(i) index
    while (current[h] != NULL)
    {
        //return true if word matched a word in the dictionary
        if (strcasecmp(word, current[h]->word) == 0)
        {
            return true;
        }
        current[h] = current[h]->next;
    }
    
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int nchars = strlen(word) - 1;
    return nchars;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    //open dictionary in read mode
    FILE *dic = fopen(dictionary, "r");
    //check if file exists
    if (dic == NULL)
    {
        return false;
    }

    char dic_word[LENGTH + 1];
    //scan each word from the dictionary
    while (fscanf(dic, "%s", dic_word) != EOF)
    {
        //create new node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        //copy the word into the node
        strcpy(n->word, dic_word);
        n->next = NULL;

        //hash the word (how many chars does it have)
        int h = hash(dic_word);

        //if no elements at the ith location`s list, insert node
        if (table[h] == NULL)
        {
            table[h] = n;
        }
        //if there are other elements in the list, insert node in the front
        else
        {
            n->next = table[h];
            table[h] = n;
        }
    }
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    //Initialize count as 0
    int count = 0;
    //initialize a temporary pointer
    node *current[N - 1];

    for (int i = 0; i < N; i++)
    {
        if (table[i] != NULL)
        {
            //make current point to table[head].
            current[i] = table[i];

            //count all the nodes for each table header
            while (current[i] != NULL)
            {
                current[i] = current[i]->next;
                count++;
            }
        }
    }
    return count;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    node *current[N - 1];
    node *temp[N - 1];
    for (int i = 0; i < N; i++)
    {
        if (table[i] != NULL)
        {
            //initialize the current node pointer to the table`s head
            current[i] = table[i];
            //initialize the temporary node pointer to the same node as current node pointer
            temp[i] = current[i];

            //delete nodes in the list and free the memory
            while (current[i] != NULL)
            {
                current[i] = current[i]->next;
                free(temp[i]);
                temp[i] = current[i];
            }
        }
    }
    
    return true;
}
  • Can't tell what the problem without seeing the declarations for all globals and the value of N. Please add the rest of the code. But a common problem when everything works except the max length test, is that code does not allow for the maximum length of a word plus the end of string marker. Did you allow for that EOS marker? – Cliff B Oct 4 at 20:15
  • i just added the globals. LENGTH is defined in a different file as #define LENGTH 45 – andream Oct 4 at 20:32
  • Still need load. – Cliff B Oct 4 at 21:09
1

After several false starts, we finally got the code to test. Have you tested your code with a 45 char word in a dictionary?

The code is generating a seg fault when the dictionary is composed of exactly 1 word of 45 chars. That seg fault is in unload(). I'm highly confident that you haven't looked for the problem there yet, so I'll give you a chance. You might even resolve the valgrind issues too, but that's just a guess. I haven't looked at the valgrind issues. Just a gut feeling. ;-)

RE: unload()
The code has an array of pointers, actually two arrays, when two single pointers are all that are needed.

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

| improve this answer | |
  • i have fixed that...i just edited and loaded in my whole current code (all functions). this is what gives me the check50 error (doesn`t handle max 45 chars words and memory leak) – andream Oct 4 at 22:50
  • thanks! i didn`t even think unload() was related to this ! i had done debug50 but not only with the 45chrs word in the dictionary ...i see now how it makes sense to test it like that...did not solve the valgrind problem though i have 1 less block of memory leak ...in use at exit: 552 bytes in 1 blocks ==1412== total heap usage: 143,096 allocs, 143,095 frees, 8,023,416 bytes allocated – andream Oct 5 at 17:31
  • oh! fclose! of course! – andream Oct 5 at 18:03

You must log in to answer this question.

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