- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是编程的新手,我一直在尝试完成 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/
我有 2 个 .cs 文件,每个文件中都有一个类。如何在 Form2.cs 中的另一个类中调用 Form1.cs 中的一个类中的方法? 看起来像这样...... Form1.cs public par
我正在尝试了解指针的移动方式。以下是程序,我知道如果 int cs={1,2,3}; 然后cs指向cs[0]我不清楚的是 *cs 指向什么。 #include int main() {
我是 ASP.NET Core 新手。我正在使用 ASP.NET Core 7。我读到在 ASP.NET Core 7 中他们删除了 Startup.cs。有人可以告诉我如何在此示例中将 Startu
所以我知道一般来说,这是不可能的,因为Jon Skeet said so . 但是我的 .cs 文件是简单的类,在顶部有一个或两个 usings。我需要一个包含所有类的文件,这样我就可以将它作为单个文
到目前为止,基本上我的脚本将值发送到网关,然后被重定向到 CS 购物车。在该页面中,我获取返回的值并对其进行操作。 我使用 fn finish 和 fn 更改订单状态来完成订单,但无论我做什么,我都会
我需要一个匹配所有以 .cs 结尾的字符串的正则表达式,但如果它们以 .g.cs 结尾,则它们不应该匹配。我正在使用 .NET 正则表达式。 最佳答案 如果是 .cs 而不是 .g.cs,这将匹配结尾
到目前为止,基本上我的脚本将值发送到网关,然后被重定向到 CS 购物车。在该页面中,我获取返回的值并对其进行操作。 我使用 fn finish 和 fn 更改订单状态来完成订单,但无论我做什么,我都会
我的 Form.cs 中有一个函数,我想在我的 program.cs 中调用它 但是如果函数不是静态的,program.cs就不能用了。如果它是静态的,则 Form.cs 无法使用它,因为它涉及非静态
因此,当我抓取不同解决方案的一些文件并将它们粘贴到不同的解决方案中时,我的 Mainform 和设计师分离了。如果我运行我的程序,表格会正确显示,但是当我在设计模式下查看我的表格时,它是一个空白表格。
我有一个用户控件 (UserControl1.ascx),我对其 cs 文件进行了更改。UserControl1.ascx 正被两个或多个使用 LoadControl 的 aspx 文件使用.我不想部
我正在学习一些 Xamarin 开发。当我研究 Xamarin 项目的例子时,like this one ,我有时会看到一个页面有一个与 xaml 文件及其代码隐藏文件同名的神秘文件,但以 *CS.c
是的,这有点毫无意义,但我想知道......我的 MVC 应用程序中所有这些代码隐藏文件都困惑了。据我所知,我需要这些文件的唯一原因是告诉 ASP.NET 我的页面是从 ViewPage 而不是 Pa
我已经从一个不再可用的人那里继承了一个网站。在已部署的文件夹中,我有 Config.aspx(请参阅代码)。但是我找不到代码隐藏文件。配置页面有效。我在哪里可以找到 .cs 文件? 谢谢
在为 Outlook(或其他潜在的 Office 程序)开发插件时,在主类上调用方法可能很有用,但是如何从事件处理程序(例如功能区中的 button_click 事件)中执行此操作。 最佳答案 使用:
我已经创建了 PlayPage.xaml、PlayPage.xaml.cs 和 Game.cs 文件。PlayPage.xaml.cs 有两个变量,windowWidth 和 windowHeight
我一直在关注使用 Caliburn Micro 的 MVVM 模式教程 https://www.youtube.com/watch?v=laPFq3Fhs8k .xaml.cs 和 ViewModel
我试图将一个值(来自一个对象)放在一个变量中,并将它放在一个表单的文本框中。 表单代码如下: public Form1(Deck mainDeck) { InitializeC
我知道这很基础,但我找不到任何关于如何在 MSDN、Google 搜索和 stackoverflow 之间执行此操作的指南/教程。 我创建了一个新的 Windows 窗体应用程序,这里我有 Progr
VS2017 15.4.1 ASP.NET MVC 5.2.3 T4MVC 4.0.0 AutoT4MVC 1.5.3 再加工者 我在这个项目中已经使用 T4MVC] 好几个月了,没有任何问题。然而,
我正在尝试配置 kestrel,以便当它处于原始模式时在特定端口上运行。但是这样做似乎 launchsettings.json 需要传递命令行参数才能这样做,因为没有直接选项并且它总是在端口 5000
我是一名优秀的程序员,十分优秀!