我正在尝试释放我的特里树上的指针。 这是我的特里结构
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);
}
这是我的 head 初始化函数
void init()
{
head = new trie;
head->x = 0;
for (int a=0; a<=25; a++)
{
head->next[a] = NULL;
}
}
程序结束后我调用了deallocate(head);
我对指针的东西真的很陌生,我的释放函数有什么问题吗?谢谢
更改了数组大小并被接受:)看来问题不是指针:)谢谢大家
最佳答案
您正在使用new
来分配内存,并使用free
来释放内存。我看到的唯一错误是您应该将 new
与 delete
一起使用,或将 malloc
与 free
一起使用。
关于c++ - 释放 trie 上的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14053056/