【发布时间】:2022-01-14 21:42:30
【问题描述】:
我正在尝试建立一个由包含指向对象的指针的节点组成的二叉树,但是在我的“清除树”函数中,当我尝试释放节点内指针处的内存时遇到了读取访问冲突。为什么我在根指针处释放内存时没有抛出异常,但在节点内的int指针处有?
抛出异常:读取访问冲突。 它是 0x2。
class Tree {
private:
struct Node {
int* val = nullptr;
Node* right = nullptr;
Node* left = nullptr;
};
Node* root = nullptr;
public:
bool Insert(int* num);
void Empty();
bool isEmpty() const;
};
void Tree::Empty()
{
while (!(root == nullptr)) // Just handling the simplest case for now
{
if (root->left == nullptr && root->right == nullptr)
{
delete root->val; // Read access violation
delete root;
root = nullptr;
break;
}
[...]
}
}
bool Tree::Insert(int* num)
{
Node* insertion = new Node;
int* temp = new int(*num);
insertion->val = temp;
if (root == nullptr)
{
root = insertion;
return true;
}
Node* c_node = root;
while (true)
{
if (*temp == *c_node->val)
{
delete temp;
delete insertion;
return false;
}
if (*temp > *c_node->val)
{
if (c_node->right != nullptr)
{
c_node = c_node->right;
continue;
}
c_node->right = insertion;
return true;
}
if (c_node->left != nullptr)
{
c_node = c_node->left;
continue;
}
c_node->left = insertion;
return true;
}
}
int main()
{
int a = 2;
Tree my_tree;
my_tree.Insert(&a);
my_tree.Empty();
}
如有任何反馈,我将不胜感激!
【问题讨论】:
-
为了让我们告诉您是否可以删除
root->val,您必须显示设置root->val的代码。另外,您确定root->val不为空吗?最好为此添加assert。您还应该显示isEmpty的定义。真的最好提供minimal reproducible example。 -
有点语义,但不要删除指针。但是你用操作符删除释放它指向的内存。如果 root 是指向节点的指针,您是否分配了它?或者它只是 Tree 的成员(注意现代 C++ 中的 new/delete 应该谨慎使用,但我认为这是作业的一部分)
-
既然可以只存储一个 int,为什么还要在 Node 中存储一个指向 int 的指针?
-
@poisson -- 使
int成为指针太过分了,对于“学习指针”的任务来说完全没有必要。您要存储的数据是int,而不是指针。数据结构(在本例中为二叉树)是通过使用指针来维护的,但这与存储在节点上的数据完全无关。
标签: c++ pointers binary-tree