У меня возникли проблемы с реализацией функций загрузки и выгрузки в pset5 класса cs50 в Гарварде. Когда я запускаю его, я получаю ошибку сегментации, а когда я запускаю valgrind, он сообщает мне, что ни один из узлов, которые я загрузил при загрузке, не был освобожден.
Я пытался исправить это в течение нескольких дней, я пробовал несколько разных реализаций для моей функции выгрузки, но ничего не сработало. Я думаю, что ошибка может быть в моей функции загрузки. Кто-нибудь, пожалуйста, пожалуйста, помогите мне с этим?
/****************************************************************************
* dictionary.c
*
* Computer Science 50
* Problem Set 5
*
* Implements a dictionary's functionality.
***************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "dictionary.h"
#define HASHTABLE_SIZE 5000
// create word counter for size
int wordCount = 0;
// linked link struct
typedef struct node
{
// word's length + NULL character
char word[LENGTH + 1];
struct node* next;
}
node;
// Hashtable array
node* hashtable[HASHTABLE_SIZE];
// hash function from study.cs50.net
int hash_function(char* key)
{
// initialize index to 0
int index = 0;
// sum ascii values
for (int i = 0; key[i] != 0; i++)
{
index += toupper(key[i]) - 'A';
}
return index % HASHTABLE_SIZE;
}
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// create variable to hold word
char temp[LENGTH + 1];
// convert every character in word to lowercase
for (int i = 0, n = strlen(word); i < n; i++)
{
if (isalpha(word[i]))
{
temp[i] = tolower(word[i]);
}
}
// get hashed word's index
int hash_index = hash_function(temp);
// find head of that index
node* head = hashtable[hash_index];
// traverse through linked list
for (node* cur = head; cur != NULL; cur = cur->next)
{
// find if linnked list contains word
if (strcmp(cur->word, word) == 0)
{
return true;
}
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// // open file
FILE* file = fopen(dictionary, "r");
// check if file exists
if (file == NULL)
{
return false;
}
// word length plus NULL character
char word[LENGTH + 1];
// iterate through every word of the dictionary
while (fscanf(file, "%s\n", word) != EOF) // Source: http://stackoverflow.com/questions/6275558/question-about-whileeof
{
node* new_node = malloc(sizeof(node));
if (new_node == NULL)
{
return false;
}
wordCount++;
strcpy(new_node->word, word); // Source: cs50 reddit
int hash_index = hash_function(new_node->word);
// check whether node should be head
if (hashtable[hash_index] == NULL)
{
hashtable[hash_index] = new_node;
new_node->next = NULL;
}
else
{
new_node->next = hashtable[hash_index];
hashtable[hash_index] = new_node;
}
}
// close file
fclose(file);
return false;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return wordCount;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
// go through all of the indexes in the hashtable
for (int i = 0; i < HASHTABLE_SIZE; i++)
{
node* head = hashtable[i];
while (head != NULL)
{
node* ptr = head->next;
free(head);
head = ptr;
}
}
return true;
}
main
и/или другой код, управляющий полной программой. В противном случае мы не знаем, звоните ли вы вообщеunload
(я не говорю, что это не так, но простые ошибки нередки в StackOverflow). - person kaylum   schedule 09.10.2015free
, и поэтому valgrind сообщает об отсутствии освобождения. Если да, то это отвлекающий маневр, и вы фокусируетесь не на том (утечке). Вместо этого сосредоточьтесь на segv. Одна из возможностей состоит в том, что вы получаете несколько длинных слов. Вы используете небезопасные функции, такие какstrcpy
иfscanf
, без проверок, чтобы убедиться, что ваши буферы не переполнены. - person kaylum   schedule 09.10.2015unload
. Хороший отладчик приведет вас прямо к источнику ошибки сегментации. - person user3386109   schedule 09.10.2015check
, которая по-прежнему имеет проблему с переполнением буфераtemp
. - person kaylum   schedule 09.10.2015LENGTH
? Возможно, этого может быть недостаточно, чтобы уместить все слова вашего файла. - person LPs   schedule 09.10.2015