C ++对象声明混乱?(C++ object declaration confusion?)

我正在尝试用C ++实现BST,我遇到了这两种创建节点的方法:

node* z = new node(); z->key = d; z->left = NULL; z->right = NULL;

然后这个:

node* y = NULL; node* x = root; node* parent = NULL;

调用new运算符有什么区别?

编辑

所以,例如,有什么区别:

node* q = new node();

node* q;

你为什么选择这样或那样的方式呢? 这种或那种方式有优势吗?

I'm trying to implement a BST in C++, and I came across these two ways of creating a node:

node* z = new node(); z->key = d; z->left = NULL; z->right = NULL;

and then this:

node* y = NULL; node* x = root; node* parent = NULL;

What is the difference in calling the new operator or not?

EDIT

so for example, whats the difference between:

node* q = new node();

and

node* q;

and why would you choose one way or the other? Is there an advantage for one way or the other?

最满意答案

要回答评论中的问题,是的,有区别。

当你执行node* q = new node()你声明一个指向node对象的指针,然后在运行时动态地为一个node对象分配足够的内存,并调用默认的构造函数(或者,value初始化对象,它取决于class / struct),最后将指向新分配的对象的指针分配给q 。

当你做node* q; 你只是声明一个指向node对象的指针,就是这样。 指针指向的内容取决于您声明指针的位置:如果将其声明为全局变量,则将其初始化为零,并变为空指针; 如果将它声明为局部变量,则其值是不确定的,并且实际上似乎是随机的。 两者都不是有效指针,并且取消引用它将导致未定义的行为 。 还有第三种选择,那就是你声明node* q; 作为class或struct的成员变量,初始值取决于如何创建和初始化类/结构(例如,如果结构是值初始化,是否有初始化它的构造函数等)。

To answer the question in the comment, yes there is a difference.

When you do node* q = new node() you declare a pointer to a node object, and then at runtime dynamically allocate memory enough for one node object, and calls the default constructor (alternatively, value initializes the object, it depends on the class/struct), and finally you assign the pointer to the newly allocated object to q.

When you do node* q; you just declare a pointer to a node object, and that's it. What the pointer will be pointing to depends on where you declare the pointer: If you declare it as a global variable, it is initialized to zero, and becomes a null pointer; If you declare it as a local variable its value is indeterminate, and will in reality seem to be random. Neither is a valid pointer, and dereferencing it will lead to undefined behavior. There is also a third alternative, and that's where you declare node* q; as a member variable in a class or struct, then the initial value depend on how the class/structure is created and initialized (for example if the structure is value initialized, if there's a constructor that initializes it, etc.).

C ++对象声明混乱?(C++ object declaration confusion?)

我正在尝试用C ++实现BST,我遇到了这两种创建节点的方法:

node* z = new node(); z->key = d; z->left = NULL; z->right = NULL;

然后这个:

node* y = NULL; node* x = root; node* parent = NULL;

调用new运算符有什么区别?

编辑

所以,例如,有什么区别:

node* q = new node();

node* q;

你为什么选择这样或那样的方式呢? 这种或那种方式有优势吗?

I'm trying to implement a BST in C++, and I came across these two ways of creating a node:

node* z = new node(); z->key = d; z->left = NULL; z->right = NULL;

and then this:

node* y = NULL; node* x = root; node* parent = NULL;

What is the difference in calling the new operator or not?

EDIT

so for example, whats the difference between:

node* q = new node();

and

node* q;

and why would you choose one way or the other? Is there an advantage for one way or the other?

最满意答案

要回答评论中的问题,是的,有区别。

当你执行node* q = new node()你声明一个指向node对象的指针,然后在运行时动态地为一个node对象分配足够的内存,并调用默认的构造函数(或者,value初始化对象,它取决于class / struct),最后将指向新分配的对象的指针分配给q 。

当你做node* q; 你只是声明一个指向node对象的指针,就是这样。 指针指向的内容取决于您声明指针的位置:如果将其声明为全局变量,则将其初始化为零,并变为空指针; 如果将它声明为局部变量,则其值是不确定的,并且实际上似乎是随机的。 两者都不是有效指针,并且取消引用它将导致未定义的行为 。 还有第三种选择,那就是你声明node* q; 作为class或struct的成员变量,初始值取决于如何创建和初始化类/结构(例如,如果结构是值初始化,是否有初始化它的构造函数等)。

To answer the question in the comment, yes there is a difference.

When you do node* q = new node() you declare a pointer to a node object, and then at runtime dynamically allocate memory enough for one node object, and calls the default constructor (alternatively, value initializes the object, it depends on the class/struct), and finally you assign the pointer to the newly allocated object to q.

When you do node* q; you just declare a pointer to a node object, and that's it. What the pointer will be pointing to depends on where you declare the pointer: If you declare it as a global variable, it is initialized to zero, and becomes a null pointer; If you declare it as a local variable its value is indeterminate, and will in reality seem to be random. Neither is a valid pointer, and dereferencing it will lead to undefined behavior. There is also a third alternative, and that's where you declare node* q; as a member variable in a class or struct, then the initial value depend on how the class/structure is created and initialized (for example if the structure is value initialized, if there's a constructor that initializes it, etc.).