gpt4 book ai didi

c++ - 在 trie 上释放指针

转载 作者:搜寻专家 更新时间:2023-10-31 01:51:12 25 4
gpt4 key购买 nike

我正在尝试取消分配我的 trie 上的指针。这是我的 trie 结构

struct trie
{
int x;
trie *next[26];
};

trie *head;
trie *tmp;

这是我使用 dfs 的释放函数

void deallocate(trie *cur)
{
for (int a=0; a<=25; a++)
{
if (cur->next[a] != NULL)
{
tmp = cur->next[a];
cur->next[a] = NULL;
deallocate(tmp);
}
}
free(cur);
}

这是我的头初始化函数

void init()
{
head = new trie;
head->x = 0;
for (int a=0; a<=25; a++)
{
head->next[a] = NULL;
}
}

程序结束后我调用了deallocate(head);

我对指针的东西真的很陌生,我的 deallocate 函数有什么问题吗?谢谢

更改数组大小并被接受:) 看来问题不是指针:) 谢谢大家

最佳答案

您正在使用 new 分配内存并使用 free 释放它。我能看到的唯一错误是您应该将 newdelete 一起使用,或者将 mallocfree 一起使用。

关于c++ - 在 trie 上释放指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14053056/

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