[英]CS50 pset5 Speller - :( program is free of memory errors valgrind tests failed; see log for more information
[英]Program is free of memory errors, valgrind tests failed - CS50 pset5 speller
我收到此錯誤 - 程序沒有內存錯誤,valgrind 測試失敗; 有關更多信息,請參閱日志。
我嘗試了很多東西,但我不斷收到更多錯誤。 這是我的代碼:
// Implements a dictionary's functionality
#include <stdbool.h>
#include <strings.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.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 = 1;
unsigned int numWords = 0;
// Hash table
node *table[N];
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
//initialize index for hashed word
int hashNum = hash(word);
//set cursor point to same address as hashtable bucket
node *cursor = table[hashNum];
//while the cursor doesn't show NULL, seach the dictionary for the word
while(cursor != NULL)
{
//the word is found if the strcasecmp returns the value of true
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
//if the word has not been found then the cursor will advance
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO
char letter = toupper(word[0]);
return ((int) letter) - ((int) 'A');
}
int word_count = 0;
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
//open the dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
//scan the dictionary word by word
char str[LENGTH + 1];
while (fscanf(file, "%s", str) != EOF)
{
node *newNode = malloc(sizeof(node));
//checks if malloc succeeded; if it is not succeeded then return false
if (newNode == NULL)
{
return false;
}
//initializes and calculates index of the word for insertion into hashtable
int hashNum = hash(str);
//copy word into node if malloc succeeds
strncpy(newNode->word, str, sizeof(str));
//initialize to point the head to hashtable of index
//node *head = hashtable[h];
//insert the new node at the beginning of the lists
if (table[hashNum] == NULL)
{
newNode->next = NULL;
}
else
{
newNode->next = table[hashNum];
}
table[hashNum] = newNode;
numWords += 1;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return numWords;;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// TODO
for (int i = 0; i < N; i++)
{
if (table[i] != NULL)
{
free(table[i]);
}
}
return true;
}
這是帶有錯誤消息的日志。 它說它來自第 76 行:
running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmpgm2cu85v -- ./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...
112 bytes in 2 blocks are still reachable in loss record 1 of 1: (file: dictionary.c, line: 76)
這是第 76 行:
node *newNode = malloc(sizeof(node));
我究竟做錯了什么?
發布的代碼無法編譯:
先解決編譯問題。
gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" -o "untitled.o"
untitled.c:25:7: error: variably modified ‘table’ at file scope
25 | node *table[N];
| ^~~~~
untitled.c: In function ‘check’:
untitled.c:31:19: warning: implicit declaration of function ‘hash’ [-Wimplicit-function-declaration]
31 | int hashNum = hash(word);
| ^~~~
untitled.c: At top level:
untitled.c:51:14: error: conflicting types for ‘hash’
51 | unsigned int hash(const char *word)
| ^~~~
untitled.c:31:19: note: previous implicit declaration of ‘hash’ was here
31 | int hashNum = hash(word);
| ^~~~
untitled.c: In function ‘hash’:
untitled.c:54:19: warning: conversion from ‘int’ to ‘char’ may change value [-Wconversion]
54 | char letter = toupper(word[0]);
| ^~~~~~~
untitled.c:55:27: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
55 | return ((int) letter) - ((int) 'A');
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~
untitled.c: In function ‘load’:
untitled.c:83:23: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Wsign-conversion]
83 | int hashNum = hash(str);
| ^~~~
untitled.c: In function ‘unload’:
untitled.c:119:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
119 | for (int i = 0; i < N; i++)
| ^
Compilation failed.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.