0

This is my program:

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.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;

// TODO: Choose number of buckets in hash table
const unsigned int N = 26;
int count_word = 0;

// Hash table
node *table[N];

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

// Hashes word to a number
unsigned int hash(const char *word)
{  int c =0;
   for(int i = 0; i<strlen(word); i++)
   {
       c+=tolower(word[i]);
   }

    // TODO: Improve this hash function
    return (c % N);
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
     FILE *file = fopen(dictionary,"r");
      if (file == NULL)
      {
        return false;
      }
      char word[LENGTH +1];
      while(fscanf(file,"%s",word) != EOF)
      {
        node *n= malloc(sizeof(node));
        if (n == NULL)
        {
            fclose(file);
            return false;
        }
        strcpy(n->word ,word);
        int index = hash(n->word);
        n->next = table[index];
        table[index]=n;
        count_word++;

      }
    // TODO
    fclose(file);
    return true;
    // TODO
    return false;
}

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

    // TODO : TO return thr number of words
    return count_word;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    for(int i=0 ;i<26;i++)
    {

      node *tmp = table[i];


      while( tmp != NULL)
      {
        node *cursor = tmp;
        cursor = cursor->next;
        free(tmp);
        tmp = cursor;

      }
      if (tmp == NULL)
      {
        return true;
      }
    }


    // TODO
    return false;
}

This is what the CS50 tool is showing:

:) dictionary.c exists
:) speller compiles
:) handles most basic words properly
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:) spell-checking is case-insensitive
:) handles substrings properly
:( program is free of memory errors
    valgrind tests failed; see log for more information.
4
  • And what exactly did valgrind show?
    – Cliff B
    Commented May 5, 2023 at 10:50
  • @CliffB hi i just posteedb the valgrind
    – shirin
    Commented May 5, 2023 at 17:48
  • 1
    so, there's over 137k memory blocks that are still reachable. Does that suggest anything to you? What exactly are you asking help for? What have you done to find the problem? What do you think is the issue?
    – Cliff B
    Commented May 5, 2023 at 21:47
  • i have tried to use severe diff ways to free in unload function but nothing is happening @Cliff B
    – shirin
    Commented May 6, 2023 at 15:31

1 Answer 1

0
speller/ $ valgrind ./speller texts/cat.txt
==4581== Memcheck, a memory error detector
==4581== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4581== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==4581== Command: ./speller texts/cat.txt
==4581== 

MISSPELLED WORDS


WORDS MISSPELLED:     0
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        6
TIME IN load:         2.01
TIME IN check:        0.03
TIME IN size:         0.00
TIME IN unload:       0.01
TIME IN TOTAL:        2.05

==4581== 
==4581== HEAP SUMMARY:
==4581==     in use at exit: 7,703,248 bytes in 137,558 blocks
==4581==   total heap usage: 143,096 allocs, 5,538 frees, 8,023,256 bytes allocated
==4581== 
==4581== 7,703,248 bytes in 137,558 blocks are still reachable in loss record 1 of 1
==4581==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==4581==    by 0x109A71: load (dictionary.c:67)
==4581==    by 0x1092DB: main (speller.c:40)
==4581== 
==4581== LEAK SUMMARY:
==4581==    definitely lost: 0 bytes in 0 blocks
==4581==    indirectly lost: 0 bytes in 0 blocks
==4581==      possibly lost: 0 bytes in 0 blocks
==4581==    still reachable: 7,703,248 bytes in 137,558 blocks
==4581==         suppressed: 0 bytes in 0 blocks
==4581== 
==4581== For lists of detected and suppressed errors, rerun with: -s
==4581== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
1
  • How does this answer the question? Is it supposed to be part of the question? You can change the question (but *** *** *** *** *** *** *** *** without *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the question should appear as if it was written today.) Commented May 13, 2023 at 12:50

You must log in to answer this question.

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