Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Difference between setting a pointer to nullptr and initializing it as a new variable type

I am learning C++ and I know the 'new' key word is used to allocate an address in memory to a pointer. And I think when using 'nullptr' initializes a pointer which points to nothing. Is that correct? Example for reference:

//using nullptr
int *x = nullptr; //this is just a pointer that points to nothing and
                  //will need to be initialized with an address before it  
                  //it can be used. Correct?

//using new
int *x = new int; //this is basically giving x an address in memory. Will the 
                  //address have some residual value stored in it or will
                  //it contain zero?

When would you use one over the other? Is new only used for dynamic memory allocation or are there other applications for it? Why would you initialize a pointer to nullptr if you could just declare it and then initialize it later?

Thanks for the help!

like image 815
Adam Halfaker Avatar asked Apr 28 '16 21:04

Adam Halfaker


3 Answers

Your understanding of new int and int *x = new int; is not correct (or at least, the way you worded it is not correct).

new int allocates some memory. It doesn't "allocate memory to a pointer", it just allocates memory. It supplies a pointer to that memory, which didn't exist previously.

int *x; also allocates memory for a variable called x. x is a variable whose value is the address of another object. (as opposed to other types of variable, whose value may be a number or a string for example).

int *x = nullptr; means x holds a special value called "null pointer" that does not indicate any other object.

int *x = new int means that x holds the address of the piece of memory that was allocated by new int. So there are two allocations involved in this line: x, and the unnamed memory allocated by new.

In the latter case you could output the address of each of these allocations separately, e.g.:

cout << &x << ',' << x << '\n';
like image 101
M.M Avatar answered Oct 25 '22 20:10

M.M


Yes, you are generally correct in your description of what those two assignments do.

As for your question about why you would initialize a pointer to NULL, that's so that your code can later test against NULL. The usual paradigm is that if a pointer is NULL, then you know it's not initialized, and not pointing to anything. If it's non-NULL, then presumably it's pointing to a valid object, and you can use it. Although that paradigm means that you need to make sure to set the pointer back to NULL when the object to which it's pointing is deleted or otherwise rendered invalid. You may want to look into how smart pointers work, as opposed to regular "dumb" pointers.

like image 1
Dan Korn Avatar answered Oct 25 '22 21:10

Dan Korn


int *x = nullptr; 

// some code here that might under some condition
// lead to an actual object address being stored in x

if (x)
{
  // do some stuff here which is only performed if 
  // x doesn't contain a nullptr value anymore
}

It is not the case that pointers do only serve a purpose when they actually point to an object. A nullpointer value is absolutely common. You can check whether a pointer is nullptr or not and that check requires the pointer to be initialized.

It doesn't have to be initialized with an actual address to be of use.

You'd want to initialize a pointer just like any other fundamental type that has indetermined value when not initialized: To give it a valid value straight away without having to keep in mind that you still need to initialize it.

A pointer variable is just like any other fundamental variable.

  • It resides on the stack having sizeof(int*) bytes of memory to store an address of type int
  • It will not be initialized (indetermined) if there is no initializer

When would you use one over the other?

The first one (nullptr): If the pointer might actually stay nullptr. The second one (new int): When you know that you'll actually need the allocated integer.

PS: Raw pointers should actually be rare nowadays (for good reasons).

like image 1
Pixelchemist Avatar answered Oct 25 '22 19:10

Pixelchemist