My code failed valgring test.I tried so many times to figure out the error and I gave up.can someone help me to find the error ..
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <cs50.h>
#include "dictionary.h"
//gloabal variables;
unsigned int word_count;
unsigned int hash_num;
// 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;
// Hash table
node *table[N];
//setting hash table to null
//for (int i = 0; i < N; i++)
//{
// table[i] = NULL;
//}
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
//hashing the word
hash_num = hash(word);
node* cursor = table[hash_num];
while(cursor != NULL)
{
if(strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO: Improve this hash function
//return toupper(word[0]) - 'A';
unsigned long total = 0;
for(int i = 0; i < strlen(word); i++)
{
total+=tolower(word[i]);
}
return total % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// open the dictionary file
FILE *source = fopen(dictionary,"r");
if(source == NULL)
{
printf("Could not open the file\n");
return false;
}
//Read each words in the file
char current_word[LENGTH+1];
while (fscanf(source, "%s", current_word) != EOF)
{
node *current_wrd = malloc(sizeof(node));
if(current_wrd == NULL)
{
return false;
}
//copy the word to node
strcpy(current_wrd->word, current_word); //cant equate 2 strings.have to use strcpy;
hash_num = hash(current_word);
current_wrd->next = table[hash_num]; //current_wrd is a ptr.it's next field is ALSE a ptr.
//next should set to what table is currently pointing
table[hash_num] = current_wrd; //now you can store the current_wrd memory address in table
//hash table is array oh Heads in linked lists
word_count++;
}
fclose(source);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
if(word_count > 0)
{
return word_count;
}
return 0;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for(int i = 0; i < N; i++)
{
node *cursor = table[i];
while(cursor)
{
node *tmp = cursor;
cursor = cursor->next;
free(tmp);
}
if(cursor == NULL)
{
free(cursor);
return true;
}
}
//free(cursor);
return false;
}
Here is the log with the error message. It says it's coming from line 96:
running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmpx3faci2p -- ./speller substring/dict substring/text...
checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"...
checking that program exited with status 0...
checking for valgrind errors...
56 bytes in 1 blocks are still reachable in loss record 1 of 1: (file: dictionary.c, line: 96)
hear is my line 96
node *current_wrd = malloc(sizeof(node));
I tried so many times to figure out the error and gave up. from where the memory leak is happening ?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742316473a4420937.html
评论列表(0条)