CS50 PSET4 无法释放拼写器中的内存
CS50 PSET4 Cant free memory in speller
我的代码运行完美,但 valgrind 显示分配给所有节点的内存仍然可以访问。这会导致 check50 的内存泄漏测试失败。
这是 valgrind 显示的内容-
堆摘要:
==14338== 在退出时使用:143,091 个块中的 8,013,096 字节
==14338== 总堆使用量:143,096 次分配,5 次释放,8,023,416 字节分配
143,091 个块中的 8,013,096 个字节在丢失记录 1 of 1 中仍然可达
这是我的代码-
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Represents number of buckets in a hash table
#define N 26
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Represents a hash table
node *hashtable[N];
//to count no. of words in the dictionary
int count = 0;
// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
// TODO
//create a new node
node *newnode = (node *)malloc(sizeof(node));
//check if new node is allocated memory successsfully
if (newnode == NULL)
{
unload();
return false;
}
//copy word from dictionary to new node
//newnode->word = word; (X) why?
strcpy(newnode->word, word);
//hash the word
int n = hash(word);
//add node to the correct bucket
newnode->next = hashtable[n];
hashtable[n] = newnode;
count++;
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
//hash the word to find its bucket
int n = hash(word);
//traverse through the bucket
node *temp = hashtable[n];
while (temp != NULL)
{
if (strcasecmp(temp->word, word) == 0)
{
return true;
}
temp = temp->next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
node *cursor;
for (int i = 0; i > 26; i++)
{
cursor = hashtable[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
}
return true;
}
在卸载和单步调试中使用 debug50 并在此行 for (int i = 0; i > 26; i++)
设置一个断点。刚刚发生了什么???该行中有错别字。悬停在下方剧透。
循环永远不会处理,因为 i
初始化为 0 且 i > 26
为 false
我的代码运行完美,但 valgrind 显示分配给所有节点的内存仍然可以访问。这会导致 check50 的内存泄漏测试失败。 这是 valgrind 显示的内容-
堆摘要:
==14338== 在退出时使用:143,091 个块中的 8,013,096 字节
==14338== 总堆使用量:143,096 次分配,5 次释放,8,023,416 字节分配
143,091 个块中的 8,013,096 个字节在丢失记录 1 of 1 中仍然可达
这是我的代码-
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Represents number of buckets in a hash table
#define N 26
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Represents a hash table
node *hashtable[N];
//to count no. of words in the dictionary
int count = 0;
// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
// TODO
//create a new node
node *newnode = (node *)malloc(sizeof(node));
//check if new node is allocated memory successsfully
if (newnode == NULL)
{
unload();
return false;
}
//copy word from dictionary to new node
//newnode->word = word; (X) why?
strcpy(newnode->word, word);
//hash the word
int n = hash(word);
//add node to the correct bucket
newnode->next = hashtable[n];
hashtable[n] = newnode;
count++;
}
// Close dictionary
fclose(file);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
//hash the word to find its bucket
int n = hash(word);
//traverse through the bucket
node *temp = hashtable[n];
while (temp != NULL)
{
if (strcasecmp(temp->word, word) == 0)
{
return true;
}
temp = temp->next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
node *cursor;
for (int i = 0; i > 26; i++)
{
cursor = hashtable[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
}
return true;
}
在卸载和单步调试中使用 debug50 并在此行 for (int i = 0; i > 26; i++)
设置一个断点。刚刚发生了什么???该行中有错别字。悬停在下方剧透。
循环永远不会处理,因为
i
初始化为 0 且i > 26
为 false