- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望有人能帮助我理解我哪里出了问题。我正在实现一个程序来检查拼写正确性。在此过程中,我使用 trie 数据结构将字典文本文件加载到内存中以检查单词。
总体而言,它似乎按预期运行,但在加载尽可能长的单词(即 pneumonoultramicrooscopysilicovolcanoconiosis)时,我遇到了很多问题。我不明白为什么,但首先让我展示一些代码 -
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
fprintf(stderr, "Could not open %s dictionary file.\n", dictionary);
return false;
}
// Initialise the root t_node
root = (t_node *) malloc(sizeof(t_node));
if (root == NULL)
{
fprintf(stderr, "Could not allocate memory to trie structure.\n");
return false;
}
// Set all current values in root to NULL and is_word to false
for (int i = 0; i < ALPHA_SIZE; i++)
{
root->branch[i] = NULL;
}
root->is_word = false;
while (1)
{
// Create char aray to hold words from .txt dictionary file once read
char *word = (char *) malloc((LENGTH + 1) * sizeof(char));
if (fscanf(dict, "%s", word) == EOF)
{
free(word);
break;
}
t_node *cursor = root;
int len = strlen(word) + 1;
for (int i = 0; i < len; i++)
{
if (word[i] == '\0')
{
cursor->is_word = true;
cursor = root;
word_count++;
}
else
{
int index = (word[i] == '\'') ? ALPHA_SIZE - 1 : tolower(word[i]) - 'a';
if (cursor->branch[index] == NULL)
{
cursor->branch[index] = (t_node *) malloc(sizeof(t_node));
for (int j = 0; j < ALPHA_SIZE; j++)
{
cursor->branch[index]->branch[i] = NULL;
}
cursor->branch[index]->is_word = false;
}
cursor = cursor->branch[index];
}
}
free(word);
}
fclose(dict);
return true;
}
这是我将字典加载到内存中的整个函数。作为引用,我定义了 trie 结构并在此函数之前创建了根。 LENGTH 定义为 45,以考虑最长的可能单词。 ALPHA_SIZE 为 27,包括小写字母和撇号。
正如我已经说过的,对于所有其他较短的单词,此功能运行良好。但对于最长的单词,该函数会处理大约一半的单词,在遇到 sysmalloc 断言问题并中止之前,会达到单词变量的索引 29。
我试图找出这里发生的情况,但我最多看到的是它的故障在于 -
cursor->branch[index] = (t_node *) malloc(sizeof(t_node));
一旦到达单词的第 29 个索引,但之前没有其他索引。我能找到的所有其他帖子都与给出此错误的函数相关,但这些函数根本不起作用,而不是大多数时候都有异常。
任何人都可以看到我看不到的内容以及我在这段代码中可能犯的错误是什么?我将不胜感激任何帮助,并感谢大家花时间考虑我的问题。
* 更新 *
首先我要感谢大家的帮助。我非常惊喜地看到有多少人回复了我的问题,而且回复得有多快!我无法表达对你们所有人的帮助的感激之情。尤其是 Basile Starynkevitch,他给了我大量信息并提供了很多帮助。
我非常尴尬地说我已经发现了我的问题,而且我应该在转向 SO 之前很长时间才发现这个问题。所以我必须为在如此愚蠢的事情上浪费大家的时间而道歉。我的问题就出在这里 -
else
{
int index = (word[i] == '\'') ? ALPHA_SIZE - 1 : tolower(word[i]) - 'a';
if (cursor->branch[index] == NULL)
{
cursor->branch[index] = (t_node *) malloc(sizeof(t_node));
for (int j = 0; j < ALPHA_SIZE; j++)
{
cursor->branch[index]->branch[j] = NULL; // <<< PROBLEM WAS HERE
}
cursor->branch[index]->is_word = false;
}
cursor = cursor->branch[index];
}
在我的代码中,最初我有“cursor->branch[index]->branch[i] = NULL”,我在该循环中迭代“int j”,而不是 i ....
再次感谢大家的帮助!对于我的问题格式不正确,我深表歉意,以后我会更好地遵守 SO 准则。
最佳答案
你的
char *word = (char *) malloc((LENGTH + 1) * sizeof(char));
后面没有对 malloc
失败进行测试;您需要添加:
if (!word) { perror("malloc word"); exit(EXIT_FAILURE); }
之前
if (fscanf(dict, "%s", word) == EOF)
自从使用 fscanf
与 %s
在NULL
上指针错误(可能是 undefined behavior )。
顺便说一句, fscanf
的最新版本(或使用 dynamic memory TR )接受 %ms
读取字符串时分配的说明符。在这些系统上您可以:
char*word = NULL;
if (fscanf(dict, "%ms", &word) == EOF))
break;
最后,使用所有警告和调试信息进行编译(gcc -Wall -Wextra -g
使用 GCC),改进代码以消除警告,并使用调试器 gdb
和 valgrind .
顺便说一句pneumonoultramicroscopesilicovolcanoconiosis有45个字母。您需要一个额外的字节作为终止 NUL (否则您将得到 buffer overflow )。所以你的LENGTH
应该至少为 46(我建议选择稍大一些的值,也许是 64;事实上,我建议系统 C dynamic memory allocation 并避免在 more robust style 中硬编码此类限制和代码,如下GNU coding standards)。
关于c- sysmalloc 断言问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47400374/
我有以下实体字段: /** * @Assert\Regex( * pattern = "/^d+\.(jpg|png|gif)$/", * htmlPattern = "/^d+\.(j
我有一个显示查询结果的表。 我使用这个检查表包含结果: cy.get("table", {timeout: 60000}).find("tr[data-index]").should("have.le
void (assert)(int e) { assert(e); } 这里是如何工作的? 最佳答案 void (assert)(int e) 相当于 void assert(int) 为什么
大家好,我的第一次测试遇到了一些问题 我正在写这个 fragment ,但我一直遇到这个问题: java.lang.AssertionError at org.junit.Assert.fail(As
从表中,我获取字符串列表,并想检查当 val!= "A"时,表列表中是否存在字符串“x”: for (int i = 0; i list = new ArrayList(); for(int i=0
我想在现有代码中为 VHDL 断言操作添加一个错误计数器。基本上,我的代码如下所示: assert data = good_data report "Bad data" severity er
我有一个无法处理的问题,所以我想也许你可以帮助我。基本上我有一个函数,它接收 char* 作为参数并对其执行一些操作(我已经检查了这些步骤/函数,它们工作得很好)。如果函数中给出的 char* 是“”
我有一些代码用于检查输入日期是否正确。只需检查是否不超过 12 个月,给定月份中的日期是否正确,并且还应该考虑闰年(因此,基本上是正常的日期检查)。唯一不合适的是年份不应低于 2000。如果日期无效,
我有一个关于 Cypress 断言的问题,最近才开始使用这个测试平台,但是当 URL 返回一个随机数时卡住了,如下所示。 /Geocortex/Essentials/REST/sites/SITE?f
我有一个 Python mock对象,我想断言是否设置了该对象的任何属性。 我不相信PropertyMock将适用于我的目的,因为我必须知道是否设置了任何属性,而不是特定属性。 它也不像我可以模拟模拟
考虑这个简单的基类 Foo 有一个函数 foo 调用一个纯虚函数 foo_,用 Doxygen 记录: class Foo { public: /** \brief This functi
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我在这段代码中执行 malloc 时遇到问题, /*function starts*/ if(NULL==(partial_results=(bignum_t**)malloc(sizeof
大家好,我想知道如何将整个代码块放入一个断言中? 例如,我有一个数组,我想对数组的每个值进行断言。这是我的代码的样子: for (int value : values) { assert Wi
这不是我的作业——我只是在练习。我似乎无法完全理解这个断言概念。 1) Determine the pre-condition for x that guarantees the post-condi
是否可以在带有时间戳的断言失败时记录信息 例如 int a = 10 assert( a > 100 ); 那么它将失败并像时间戳一样输出 2013-12-02 , 17:00:05 assert f
由于我们公司不使用单元测试,所以我正在自学对自己的代码进行单元测试。我正在使用标准的 .net 测试框架进行一些非常基本的单元测试。 我的一个方法返回一个 IEnumerable我想测试它的输出。所以
我有一个如下所示的对象数组,并且只想针对数组中的某些字段进行断言。 对象数组 - var a = [{"key":"key2","value":"value2","updatedBy":"name1"
我正在尝试编写一个 PSL 断言来检查输入断言的数量是否与输出断言的数量相匹配。 例如: . 在输入上任何时候都可能发生任何事情,输出也可以在任何时候断言。确切时间未知,也不重要。我要验证的是没有信息
我想在Cypress中做一个断言如下: cy.get(a).should('be.visible').or(()=>{ cy.get(b).should('be.visible'); });
我是一名优秀的程序员,十分优秀!