释放trie上的指针
Deallocate pointer on trie
我试图在我的树上释放指针。这是我的结构体
struct trie
{
int x;
trie *next[26];
};
trie *head;
trie *tmp;
下面是使用dfs
的deallocate函数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 init函数
void init()
{
head = new trie;
head->x = 0;
for (int a=0; a<=25; a++)
{
head->next[a] = NULL;
}
}
,程序结束后我叫它deallocate(head);
我真的是新的指针的东西,我的deallocate函数有什么问题吗?由于
改变数组大小并被接受:)似乎问题不是指针:)谢谢大家
您使用new
来分配内存,使用free
来释放内存。我能看到的唯一错误是,您应该使用new
与delete
,或malloc
与free
。
您的函数对于空输入是不正确的。deallocate(NULL)
将崩溃。函数(尤其是构成框架的一般函数)应该是自给自足的,并且应该能够覆盖所有可能的输入。
-
deallocate需要处理NULL输入
if (cur == NULL){返回;}
功能开始
-
*head应该在声明
时声明为NULL
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 为什么此代码在此 Trie 实现中使用映射 c++ 中的指针崩溃?
- 正在插入Trie,NULL指针
- 释放trie上的指针