gpt4 book ai didi

c - 字符串内存问题 : printing a string with printf seems to overwrite itself

转载 作者:行者123 更新时间:2023-12-04 14:30:36 26 4
gpt4 key购买 nike

我开始使用 MIT OCW 入门类(class)学习 C(该类(class)可以在 here 中找到)。我正在完成一项作业,其中您必须使用给定模板(在名为 prob2.c 的文件中找到 here)编写函数来将 trie 结构实现为英法词典。基本上,该程序应该要求翻译一个单词,在 trie 中查找它,然后给出翻译。我首先尝试自己实现它,但我一直遇到一个错误,结果似乎是打印“覆盖”了自己。这是一个正在发生的事情的例子,因为解释起来有点奇怪:

Enter word to translate: Adam
,Adamo> Adam
Enter word to translate: word
,parole mot
Enter word to translate: hello
,eh oh-> salut
Enter word to translate: tree
tree -> arbre

对于这些输入,它应该看起来像这样,以便翻译仅由逗号分隔:

Enter word to translate: Adam
Adam -> Adam,Adamo
Enter word to translate: word
word -> mot,parole
Enter word to translate: hello
hello -> salut,allo,eh oh
Enter word to translate: tree
tree -> arbre

在放弃并查看解决方案(找到 here ,参见问题 6.2,完整的问题描述也包括在内)之后,我的代码与给出的代码没有太大区别。无论如何,我使用 OCW 上发布的解决方案再次编译,似乎没有任何变化。我尝试使用 valgrind 运行该程序,看看是否有任何明显的内存问题可以解释这一点,但是在查找单词时没有出现任何问题,只是在最后释放 trie 时出现了一些问题。

这是我怀疑可能是主要问题的功能(这是来 self 在帖子前面链接的解决方案):

/* add word to trie, with translation
input: word and translation
output: non-zero if new node added, zero otherwise
postcondition: word exists in trie */
int add_word(const char *word, char *translation) {
struct s_trie_node *pnode = proot;
int i, len = strlen(word), inew = 0;
unsigned char j;
for (i = 0; i < len; i++) {
j = word[i];
if ((inew = !pnode->children[j]))
pnode->children[j] = new_node();
pnode = pnode->children[j];
}
if (pnode->translation) {
/* concatenate strings */
char *oldtranslation = pnode->translation;
int oldlen = strlen(oldtranslation), newlen = strlen(translation);
pnode->translation = malloc(oldlen + newlen + 2);
strcpy(pnode->translation, oldtranslation);
strcpy(pnode->translation + oldlen, ",");
strcpy(pnode->translation + oldlen + 1, translation);
free(oldtranslation);
} else
pnode->translation = strcpy(malloc(strlen(translation) + 1), translation);
return inew;
}

如果有人能向我解释为什么会发生这种情况以及如何解决它,我将不胜感激!

最佳答案

您的翻译正在以 CRLF 结尾输入,但解析代码正在寻找 LF 终止符。因此,每个翻译的末尾都有一个 CR,就在 add_word 添加的逗号之前。因此,当您打印它们时,CR 会使它们在屏幕的左边缘一个接一个地打印。

add_word 中添加测试以确认这一点。

if (pnode->translation[oldlen-1] == '\r') ...

关于c - 字符串内存问题 : printing a string with printf seems to overwrite itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868284/

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