数据结构C
好的,我这样定义我的结构。
struct trie {
struct trie *child[26];
int count;
char letter;
};
问题是当我尝试用单词填充我的字典树时,我遇到了分段错误。 我被告知问题是子变量没有指向任何东西,将它们设置为 NULL 可以解决这个问题。创建第二个结构也是实现这一目标的好方法。我是 C 编程新手,对如何创建第二个结构来实现这一目标感到困惑。任何帮助将不胜感激。
int addWordOccurrence(const char* word)
{
struct trie *root;
root = (struct trie *)malloc(sizeof(struct trie*));
struct trie *initRoot=root;
int count;
int x=strlen(word);
printf("%d",x);
int i;
for(i=0; i<x; i++)
{
int z=word[i]-97;
if(word[i]=='\n')
{
z=word[i-1]-97;
root->child[z]->count++;
root=initRoot;
}
root->child[z] = (struct trie *)malloc(sizeof(struct trie));
root->child[z]->letter=word[i];
root->child[z]=root;
}
return 0;
}
ok so i define my structure like this.
struct trie {
struct trie *child[26];
int count;
char letter;
};
the problem is when i try to fill my trie with words i get a segmentation fault.
ive been told that the problem is that the child variable isn't pointing to anything and setting them to NULL would fix this. Also creating a second structure would be a good way to achieve this. i am new to C programming and am confused on how to create a second structure to achieve this. any help would be much appreciated.
int addWordOccurrence(const char* word)
{
struct trie *root;
root = (struct trie *)malloc(sizeof(struct trie*));
struct trie *initRoot=root;
int count;
int x=strlen(word);
printf("%d",x);
int i;
for(i=0; i<x; i++)
{
int z=word[i]-97;
if(word[i]=='\n')
{
z=word[i-1]-97;
root->child[z]->count++;
root=initRoot;
}
root->child[z] = (struct trie *)malloc(sizeof(struct trie));
root->child[z]->letter=word[i];
root->child[z]=root;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是有问题的。
1) 如果
child[z]
已经设置了怎么办?2) 你永远不会将
child[z]->child
或child[z]->count
设置为#2 导致段错误的任何内容,#1 是内存泄露。
我的解决方案是编写一个用于分配新子级的函数:
然后您的代码将变为:
您还必须更改 root 的 malloc:
这更清晰,并且避免了我提到的错误。 http://codepad.org/J6oFQJMb 大约 6 次调用后没有段错误。
我还注意到您的代码
malloc
sa newroot
,但从未返回它,因此除了此函数之外没有人可以看到它。这也是内存泄漏。This is problematic.
1) What if
child[z]
was already set?2) You never set
child[z]->child
orchild[z]->count
to anything#2 is causing your segfaults, #1 is a memory leak.
My solution would be to write a function for allocating new children:
Then your code would become:
You also have to change the malloc of root:
Which is more clear, and avoids the errors I mentioned. http://codepad.org/J6oFQJMb No segfaults after 6 or so calls.
I've also noticed that your code
malloc
s a newroot
, but never returns it, so nobody except this function can ever see it. This is also a memory leak.除了@MooingDuck的答案之外,你的代码还有另一个问题:
你做了一个
,但你真正的意思是分配`sizeof(struct trie),而不是指针的sizeof(如果你是的话,它可能是4或8)在 x86 或 x86_64 上)。
这更好(不需要在 C 中显式转换
malloc
的返回指针,并且可以像这样执行sizeof
:In addition to @MooingDuck's answer, there is another problem with your code here:
You did a
but you really mean to allocate the `sizeof(struct trie), and not sizeof a pointer (which will likely be 4 or 8 if you're on x86 or x86_64).
This is better (don't need explicit cast of
malloc
's return pointer in C, and you can dosizeof
like this: