C++中new的使用
The usage of new in C++
超级愚蠢的问题,我想用c++创建一个二叉树,下面是我的代码
include <iostream>
using namespace std;
struct tree{
tree * child_left = NULL;
tree * child_right = NULL;
int root;
tree * parent = NULL;
};
int main(int argc, const char * argv[]) {
tree *t1 = new tree;
tree *t2 = new tree;
tree *t3 = new tree;
tree *t4 = new tree;
tree *t5 = new tree;
tree *t6 = new tree;
tree *t7 = new tree;
t4->root = 1;
t5->root = 3;
t6->root = 6;
t7->root = 9;
t2->root = 2;
t2->child_left = t4;
t2->child_right = t5;
t3->root = 7;
t3->child_left = t6;
t3->child_right = t7;
t1->root = 4;
t1->child_left = t2;
t1->child_right = t3;
cout << t1->child_left->child_right->root;
return 0;
}
这实际上可以工作,但是如果我在声明这些节点时删除了新节点,xcode 将出现类似 (Thread1: EXC_BAD_ACCESS(code = 1, address = 0x10)) 的错误。
我想知道是什么原因导致线程问题以及为什么在声明这些节点时需要使用 new。
提前致谢。
您正在声明指向树对象的指针。如果不使用 new,您就不会为这些指向的指针分配任何对象。
int main(int argc, const char * argv[]) {
tree t1, t2, t3, t4, t5, t6, t7;
t4.root = 1;
t5.root = 3;
t6.root = 6;
t7.root = 9;
t2.root = 2;
t2.child_left = &t4;
t2.child_right = &t5;
t3.root = 7;
t3.child_left = &t6;
t3.child_right = &t7;
t1.root = 4;
t1.child_left = &t2;
t1.child_right = &t3;
cout << t1.child_left->child_right->root;
return 0;
}
这负责在堆栈上创建对象,然后在作用域结束时自动销毁这些对象。
tree
可能应该有一个接受参数的构造函数,以便您创建一个对象而不是一袋数据。
因为new
关键字实际上创建了指针指向的对象。没有 new
关键字,指针未初始化,取消引用会导致未定义的行为。
此外,正确编写的代码必须始终 delete
所有使用 new
实例化的对象,以避免内存泄漏。在这个简单的例子中不需要,但最好早点养成良好的习惯。
初始化指针时需要 new 语句,因为所有 node/tree 变量都是指针。由于所有数据结构的性质(元素的动态数量,可以对数据结构执行的操作几乎总是依赖于堆分配的值等),在开发数据结构时使用指针是很常见的。
我想我应该指出,当您使用关键字 new
时,您实际上是在堆上创建一个变量,并在堆栈上创建一个指向该变量的指针。如果你没有使用 new
这个词,你只会在堆栈上创建一个指针,而不是创建一个变量。
在 C++ 中,每当您在堆上分配某些内容时,您还必须记住在不再需要它时将其删除。与堆栈上的变量不同,这些变量不会自动删除,因为它们永远不会超出范围。您需要为此使用关键字 delete
否则您将得到 'memory leaks'.
我建议您看一下这个快速教程来阐明您的动态规划概念:http://www.cplusplus.com/doc/tutorial/dynamic/ .
希望这能让您对正在发生的事情有所了解!
超级愚蠢的问题,我想用c++创建一个二叉树,下面是我的代码
include <iostream>
using namespace std;
struct tree{
tree * child_left = NULL;
tree * child_right = NULL;
int root;
tree * parent = NULL;
};
int main(int argc, const char * argv[]) {
tree *t1 = new tree;
tree *t2 = new tree;
tree *t3 = new tree;
tree *t4 = new tree;
tree *t5 = new tree;
tree *t6 = new tree;
tree *t7 = new tree;
t4->root = 1;
t5->root = 3;
t6->root = 6;
t7->root = 9;
t2->root = 2;
t2->child_left = t4;
t2->child_right = t5;
t3->root = 7;
t3->child_left = t6;
t3->child_right = t7;
t1->root = 4;
t1->child_left = t2;
t1->child_right = t3;
cout << t1->child_left->child_right->root;
return 0;
}
这实际上可以工作,但是如果我在声明这些节点时删除了新节点,xcode 将出现类似 (Thread1: EXC_BAD_ACCESS(code = 1, address = 0x10)) 的错误。
我想知道是什么原因导致线程问题以及为什么在声明这些节点时需要使用 new。
提前致谢。
您正在声明指向树对象的指针。如果不使用 new,您就不会为这些指向的指针分配任何对象。
int main(int argc, const char * argv[]) {
tree t1, t2, t3, t4, t5, t6, t7;
t4.root = 1;
t5.root = 3;
t6.root = 6;
t7.root = 9;
t2.root = 2;
t2.child_left = &t4;
t2.child_right = &t5;
t3.root = 7;
t3.child_left = &t6;
t3.child_right = &t7;
t1.root = 4;
t1.child_left = &t2;
t1.child_right = &t3;
cout << t1.child_left->child_right->root;
return 0;
}
这负责在堆栈上创建对象,然后在作用域结束时自动销毁这些对象。
tree
可能应该有一个接受参数的构造函数,以便您创建一个对象而不是一袋数据。
因为new
关键字实际上创建了指针指向的对象。没有 new
关键字,指针未初始化,取消引用会导致未定义的行为。
此外,正确编写的代码必须始终 delete
所有使用 new
实例化的对象,以避免内存泄漏。在这个简单的例子中不需要,但最好早点养成良好的习惯。
初始化指针时需要 new 语句,因为所有 node/tree 变量都是指针。由于所有数据结构的性质(元素的动态数量,可以对数据结构执行的操作几乎总是依赖于堆分配的值等),在开发数据结构时使用指针是很常见的。
我想我应该指出,当您使用关键字 new
时,您实际上是在堆上创建一个变量,并在堆栈上创建一个指向该变量的指针。如果你没有使用 new
这个词,你只会在堆栈上创建一个指针,而不是创建一个变量。
在 C++ 中,每当您在堆上分配某些内容时,您还必须记住在不再需要它时将其删除。与堆栈上的变量不同,这些变量不会自动删除,因为它们永远不会超出范围。您需要为此使用关键字 delete
否则您将得到 'memory leaks'.
我建议您看一下这个快速教程来阐明您的动态规划概念:http://www.cplusplus.com/doc/tutorial/dynamic/ .
希望这能让您对正在发生的事情有所了解!