1

My code is working fine and Check50 is also passing it. However I have not implemented Unload function and I am wondering why Valgrind or Check50 showing any error? All i did was changed return False to return True in Unload().

Below is the result from valgrind:

///// And Below is Valgrind Result :

==4950== Memcheck, a memory error detector
==4950== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4950== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4950== Command: ./speller
==4950== 
Usage: speller [dictionary] text
==4950== 
==4950== HEAP SUMMARY:
==4950==     in use at exit: 0 bytes in 0 blocks
==4950==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4950== 
==4950== All heap blocks were freed -- no leaks are possible
==4950== 
==4950== For counts of detected and suppressed errors, rerun with: -v
==4950== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

My code is below:

/****************************************************************************
 * 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"
//#define DICTIONARY "/home/cs50/pset5/dictionaries/large"

//#define DICTIONARY "/home/cs50/pset5/dictionaries/small"

//#define DICTIONARY "/home/ubuntu/workspace/pset5/dictionary.txt"

#define hashsize  150000 //100//

int wcount=0;





 /////// Create Hash Table ///////

 typedef struct node
 {
     char word[LENGTH+1];
     struct node *next;   // Notice struct is mentioned with the node. It is  : struct node *next , And not :node *next
 } node;
 //HashTable of Right size
 node* hashtable[hashsize];


//// Hashing Function////////

int hashfun(const char* key)
{
    int size= hashsize;

    int len = strlen (key);
    int sum=0;
    for(int i=0; i<len;i++)
    {
      sum= sum + key[i] ;

    }
    sum= sum % size;
    return sum;
}
////// Initialize HashTable////
void initialize()
{
 for (int init=0;init<hashsize-1;init++)
 {
       node* head_node=malloc(sizeof(node));
    strcpy (head_node->word, "00blank");
    head_node->next=NULL;
     hashtable[init]=head_node;
 }  

   }


///Printing (Additional)//

void print()
{
  for (int i=0;i<99;i++)   
  {  //  printf("Printing Started");
     node* printer=malloc (sizeof(node));
     printer=hashtable[i];

     printf(" %d :)",i);
    while (printer->next!=NULL)
        {
        printf("%s ->" ,printer->word);
         printer=printer->next;
        }


      if(strcmp(printer->word,"00blank")==0 && ((printer->next==NULL)))
      {
         printf("-\n") ;
      }    

      else 
      {
        printf("%s \n" ,printer->word);
      }


   //printf("The word is %s and hashkey = %d \n", printer->word,i); //print the last element
 }

}


//// Insert Function////////
void insert(char* dword, int hash)
{
  if (strcmp (hashtable[hash]->word, "00blank")==0)//0 means true for strcmp
   {
       node* new_node=malloc(sizeof(node));
       strcpy (new_node->word, dword);
       new_node->next=NULL;

       hashtable[hash]=new_node;
      wcount++;
   }

  else {   
    node* beta_node=malloc(sizeof(node));
    strcpy (beta_node->word, dword);
    beta_node->next = hashtable[hash];
    hashtable[hash]=beta_node;
     wcount++;
     }

}





////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Returns true if word is in dictionary else false.
 */
/* 
 bool check(const char* word)
 {
   int len = strlen(word);   
  char lword[LENGTH+1];
 //char *lword=NULL;
printf("Searchin ---------------------------------------------------------------------------------------------> %s \n", word);

    // int len = strlen(word);
     for(int k=0; k<len; k++)    //k=len so null terminator gets copies
     {
          lword[k] = tolower(word[k]);
     }
     lword[len]='\0';

printf("LowerCase ---------------------------------------------------------------------------------------------> %s \n", lword);
     return 1;
 }
*/ 



bool check(const char* word)///////////////////////////////////////////
{
    ////to lower case
       int len = strlen(word);   
  char lword[LENGTH+1];
 //char *lword=NULL;
//printf("Searchin ---------------------------------------------------------------------------------------------> %s \n", word);

    // int len = strlen(word);
     for(int k=0; k<len; k++)    //k=len so null terminator gets copies
     {
          lword[k] = tolower(word[k]);
     }
     lword[len]='\0';


    ////

    //word= "water";
    //printf("Searchin ---------------------------------------------------------------------------------------------> %s \n", word);
    int location=hashfun (lword);
    node* searcher=malloc (sizeof(node));
 int i =location;  
 // for (int i=0;i<hashsize-1;i++)   
     searcher=hashtable[i];
     int element=0;
         while (strcmp (searcher->word, lword)!=0 && (searcher->next!=NULL))
        {
           searcher=searcher->next;  
           element++;
        }
       // printf("while loop ended\n");
        ///// Found word or NULL
        if (strcmp(searcher->word, lword)==0) //Found word//
          {
             // printf("if (strcmp(searcher->word, word)==0) //\n");

     //        printf("The word %s is found at hashkey = %d  , element = %d \n", word,i, element);
             return true;
          }
        //
         else if (searcher->next==NULL)// Reached NULL , Goto Next Hash Index
         {// printf("  else if (searcher->next==NULL)\n");

            element=0;        // Reset Element
         }

//  printf("NOT FOUND-------------------------------\n");
    return false;
}



/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{

    // 1. Open Dictionary
      char dword[LENGTH+1]; // dword is dictionary word
        char dpath[200];
        strcpy (dpath,dictionary);

    FILE* dptr = fopen(dpath, "r");
         if (dptr== NULL)
    {
        printf("Error file missing\n");
        return 0;
    }

 // 2. Initialize hash table
 initialize();


 //2. Load Words one by one
 while (fscanf(dptr, "%s", dword)==1)
      {
    int hash =  hashfun(dword); 
    //printf("The word is %s and hashkey = %d \n", dword,hash);
    insert(dword,hash);
    }


   //   printf("The word count is -----------------------------> %d\n", wcount);
    // Hash Function
  // print();
    fclose(dptr);
    return true;
}

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

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void)
{
    // TODO
    return true;
}
3

Totally awesome that you are asking the question even though it passed check50! Kudos.

Notice this line Usage: speller [dictionary] text in the valgrind report. Speller didn't really run. You called valgrind but did not supply arguments to speller. Therefore, it didn't do anything, therefore no leaks. If you call valgrind with arguments to speller, you will see a totally different result.

This valgrind call valgrind -v --leak-check=full --show-leak-kinds=all --track-origins=yes will give all kinds of great feedback.

You must log in to answer this question.

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