我的Speller程序运行正常,只是内存泄漏似乎无法解决。我已经满足了所有其他的check50要求,非常感谢有人对我的代码逻辑的帮助。非常感谢任何愿意花一些时间来帮助我的人!无论如何,这是代码:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 = 1000;
// Hash table
node *table[N];
// File pointer
FILE *read;
// Node pointer
node *new_node;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// Input word into hash function
int word_index = hash(word);
// Check if word is in dictionary
for (node *tmp = table[word_index]; tmp != NULL; tmp = tmp->next)
{
if (strcasecmp(word, tmp->word) == 0)
{
return true;
}
}
return false;
}
// Hashes word to a number
// Credits to Neel Mehta from http://stackoverflow.com/questions/2571683/djb2-hash-function
unsigned int hash(const char *word)
{
unsigned long index = 5381;
for (const char *ptr = word; *ptr != '\0'; ptr++)
{
index = ((index << 5) + index) + tolower(*ptr);
}
return index % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize word
char dict_word[LENGTH + 1];
// Open file for reading
read = fopen(dictionary, "r");
if (read == NULL)
{
// Terminate program
printf("Cannot open dictionary\n");
return false;
}
// Loop until end of file
while (fscanf(read, "%s", dict_word) != EOF)
{
// Initialize node pointer
new_node = malloc(sizeof(node));
if (new_node == NULL)
{
// Terminate program
free(new_node);
printf("Insufficient memory storage\n");
return false;
}
// Copy word into node pointer
strcpy(new_node->word, dict_word);
// Set pointer to null
new_node->next = NULL;
// Call upon hash function
int word_index = hash(new_node->word);
// Index result into hash table
if (table[word_index] == NULL)
{
// Open node
table[word_index] = new_node;
}
else
{
// !Open node
new_node->next = table[word_index];
table[word_index] = new_node;
}
}
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// Iterate through the whole hash table
int word_counter = 0;
for (int i = 0; i < N; i++)
{
if (table[i] == NULL)
{
// Skip iteration
}
else
{
// Check how many words
for (node *tmp = table[i]; tmp != NULL; tmp = tmp->next)
{
word_counter++;
}
}
}
return word_counter;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// Iterate through the hash function
for (int i = 0; i < N; i++)
{
if (table[i] == NULL)
{
// Skip iteration
}
else
{
// Initialize pointers
node *tmp_first = table[i];
node *tmp_second = table[i]->next;
// Iterate through the linked list
while (tmp_second != NULL)
{
free(tmp_first);
tmp_first = tmp_second;
tmp_second = tmp_second->next;
}
}
}
free(new_node);
fclose(read);
return true;
}
Valgrind说,仍然可以在1块中达到56个字节,具体是指:
new_node = malloc(sizeof(node));
位于加载功能中。我感到困惑的是最后我释放了它:
free(new_node);
但似乎没有效果。这是我能够提交和获得完美分数之前剩下的唯一问题,我想理解为什么在那一行中仍然存在内存泄漏。再次感谢!
两个问题:
new_node
不应该是全球性的。它应该在load
函数本地。
释放内存时,您不会释放每个链接列表中的最后一个元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句