数据结构C

发布于 2024-12-12 16:16:58 字数 911 浏览 1 评论 0原文

好的,我这样定义我的结构。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-12-19 16:16:59
root->child[z] = (struct trie *)malloc(sizeof(struct trie));
root->child[z]->letter=word[i];
root->child[z]=root;

这是有问题的。
1) 如果 child[z] 已经设置了怎么办?
2) 你永远不会将 child[z]->childchild[z]->count 设置为

#2 导致段错误的任何内容,#1 是内存泄露。

我的解决方案是编写一个用于分配新子级的函数:

struct trie* newtrie(char newchar) {
    struct trie* r = malloc(sizeof(struct trie));
    memset(r, 0, sizeof(struct trie));
    r->letter = newchar;
    return r;
}

然后您的代码将变为:

    if (root->child[z] == NULL)
        root->child[z] = newtrie(word[i]);
    root->child[z]=root;

您还必须更改 root 的 malloc:

struct trie *root = newtrie(0);

这更清晰,并且避免了我提到的错误。 http://codepad.org/J6oFQJMb 大约 6 次调用后没有段错误。

我还注意到您的代码 mallocsa new root,但从未返回它,因此除了此函数之外没有人可以看到它。这也是内存泄漏。

root->child[z] = (struct trie *)malloc(sizeof(struct trie));
root->child[z]->letter=word[i];
root->child[z]=root;

This is problematic.
1) What if child[z] was already set?
2) You never set child[z]->child or child[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:

struct trie* newtrie(char newchar) {
    struct trie* r = malloc(sizeof(struct trie));
    memset(r, 0, sizeof(struct trie));
    r->letter = newchar;
    return r;
}

Then your code would become:

    if (root->child[z] == NULL)
        root->child[z] = newtrie(word[i]);
    root->child[z]=root;

You also have to change the malloc of root:

struct trie *root = newtrie(0);

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 mallocs a new root, but never returns it, so nobody except this function can ever see it. This is also a memory leak.

木緿 2024-12-19 16:16:59

除了@MooingDuck的答案之外,你的代码还有另一个问题:

int addWordOccurrence(const char* word)
{

    struct trie *root;
    root = (struct trie *)malloc(sizeof(struct trie*));
    struct trie *initRoot=root;
    int count;
    /* ... */
}

你做了一个

root = (struct trie *)malloc(sizeof(struct trie*));

,但你真正的意思是分配`sizeof(struct trie),而不是指针的sizeof(如果你是的话,它可能是4或8)在 x86 或 x86_64 上)。

这更好(不需要在 C 中显式转换 malloc 的返回指针,并且可以像这样执行 sizeof

struct tree *root = malloc(sizeof(*root));

In addition to @MooingDuck's answer, there is another problem with your code here:

int addWordOccurrence(const char* word)
{

    struct trie *root;
    root = (struct trie *)malloc(sizeof(struct trie*));
    struct trie *initRoot=root;
    int count;
    /* ... */
}

You did a

root = (struct trie *)malloc(sizeof(struct trie*));

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 do sizeof like this:

struct tree *root = malloc(sizeof(*root));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文