0

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?

ocean800
  • 3,489
  • 13
  • 41
  • 73
  • `NULL` means it doesn't point to any location, and I think `root` is just another node that is created with `node* root = new node();` – moffeltje Jul 01 '15 at 17:11
  • well I guess my question is, is there a difference in declaring a node like this: `node* q = new node();` and `node* q;` – ocean800 Jul 01 '15 at 17:13
  • 1
    Sure, one initializes `q` with a value, the other doesn't and results in a [*wild pointer*](http://stackoverflow.com/questions/2583656/what-is-the-meaning-of-wild-pointer-in-c) – scohe001 Jul 01 '15 at 17:13
  • 1
    `node *q;` doesn't have a valid reference to an object `node` – Ediac Jul 01 '15 at 17:16
  • 1
    BTW if you are using C++11 or newer please use `nullptr` and not `NULL` – NathanOliver Jul 01 '15 at 17:18
  • 1
    And to expand on the comment by @NathanOliver, if you're using an earlier version of C++, then it's recommended to use `0` instead of `NULL`. – Some programmer dude Jul 01 '15 at 17:19
  • @NathanOliver Thanks I'll keep that in mind, but whats the reason for that? – ocean800 Jul 01 '15 at 17:20
  • 1
    @ocean800 The reason is `int a = NULL;` will compile while `int a = nullptr;` will not. See this: http://stackoverflow.com/questions/13816385/what-are-the-advantages-of-using-nullptr – NathanOliver Jul 01 '15 at 17:21

2 Answers2

3

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.).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

I'm pretty sure (Someone correct me if i'm wrong), using the new operator allocates dynamic memory for that variable. The variables will most likely already have some garbage value assigned to them that gets overridden when you assign a new value to them. Whereas assigning it NULL just assigns it whatever the NULL value is (probably 0000000).

AverageWorker
  • 546
  • 1
  • 4
  • 13