gpt4 book ai didi

CS50 Speller 可以编译,但根本无法运行

转载 作者:行者123 更新时间:2023-12-05 06:51:54 25 4
gpt4 key购买 nike

我是编程的新手,我一直在尝试完成 cs50 类(class),而不只是复制其他人的代码并试图理解为什么我的代码不起作用。我目前被困在 pset5 中(我已经待了几天),代码编译正常,但无法正常工作并且 valgrind 返回:

Process terminating with default action of signal 11 (SIGSEGV)
==1697== Access not within mapped region at address 0x0
==1697== at 0x401A86: hash (dictionary.c:53)
==1697== by 0x401B71: load (dictionary.c:78)
==1697== by 0x4012BE: main (speller.c:40)

这是我的代码:

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"

/*
** Global variable that indicates the number of words loaded in the dictionary
*/
unsigned int loadedWords = 0;

// 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 = 54;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
if (table[hash(word)]->next == NULL)
{
return false;
}
else if (strcasecmp(word, table[hash(word)]->word) == 0)
{
return true;
}
else
{
return check(table[hash(word)]->next->word);
}
}

// Hashes word to a number
unsigned int hash(const char *word)
{
char lowerCaseWord[LENGTH + 1];
for (int i = 0; word[i]; i++)
{
lowerCaseWord[i] = tolower(word[i]);
}
unsigned int hashNumber = ((int)word[0] + (int)word[1]) % 53;
return hashNumber;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
printf("Could not open\n");
return false;
}
char word[LENGTH + 1];
while (fscanf(file, "%s", word) != EOF)
{
node *buffer = malloc(sizeof(node));
if (buffer == NULL)
{
return false;
}
strcpy(buffer->word, word);
buffer->next = table[hash(word)]->next;
table[hash(word)]->next = buffer;
loadedWords ++;
}
fclose(file);
return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
return loadedWords;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N - 1; i++)
{
while (table[i] != NULL)
{
node *tmp = table[i]->next;
free(table[i]);
table[i] = tmp;
}
}
return true;
}

有人可以指出为什么这不起作用和/或如何解决这个问题吗? 我不是在寻找问题的完整解决方案,我只是想知道我的错误是什么或者我的逻辑是怎么错的。谢谢。

最佳答案

关于 OPs 声明:代码编译正常,

这是编译器的输出。这清楚地表明发布的代码无法编译,

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled1.c" -o "untitled1.o" 

untitled1.c:26:7: error: variably modified ‘table’ at file scope
26 | node *table[N];
| ^~~~~

untitled1.c: In function ‘check’:

untitled1.c:31:15: warning: implicit declaration of function ‘hash’ [-Wimplicit-function-declaration]
31 | if (table[hash(word)]->next == NULL)
| ^~~~

untitled1.c: At top level:

untitled1.c:46:14: error: conflicting types for ‘hash’
46 | unsigned int hash(const char *word)
| ^~~~

untitled1.c:31:15: note: previous implicit declaration of ‘hash’ was here
31 | if (table[hash(word)]->next == NULL)
| ^~~~

untitled1.c: In function ‘hash’:

untitled1.c:51:28: warning: conversion from ‘int’ to ‘char’ may change value [-Wconversion]
51 | lowerCaseWord[i] = tolower(word[i]);
| ^~~~~~~

untitled1.c:53:31: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
53 | unsigned int hashNumber = ((int)word[0] + (int)word[1]) % 53;
| ^

untitled1.c:48:10: warning: variable ‘lowerCaseWord’ set but not used [-Wunused-but-set-variable]
48 | char lowerCaseWord[LENGTH + 1];
| ^~~~~~~~~~~~~

untitled1.c: In function ‘unload’:

untitled1.c:92:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
92 | for (int i = 0; i < N - 1; i++)
| ^

Compilation failed.

关于:

char lowerCaseWord[LENGTH + 1];
for (int i = 0; word[i]; i++)
{
lowerCaseWord[i] = tolower(word[i]);
}

数组lowerCaseWord[]没有在任何地方使用,所以这 block 代码可以去掉

关于CS50 Speller 可以编译,但根本无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66056805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com