Reputation: 61
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!
Upvotes: 6
Views: 19804
Reputation: 33932
int *x = nullptr;
You are sort-of correct. The value of x
is nullptr
and nullptr
is... Uhm... Let's ask the experts: What exactly is nullptr? Anyway it's not quite nothing. It's just guaranteed to be a safe parking space until the pointer can be put to better use.
int *x = new int
Reading the value pointed to by x
, *x
will be Undefined Behaviour. You will probably get a residual value, but who can say for sure?
int *x = new int();
Reading *x
will get 0.
int *x = new int(1000000);
Reading *x
will get 1000000.
If you don't have anything to point at, use nullptr
as a placeholder. Don't allocate storage just to give the pointer something to point at. It's wasted memory, it's wasted time allocating and deallocating, and it's a potential memory leak if you forget to delete
it. nullptr
is safe. You can test for nullptr
and make decisions about the quality of the information it points at. You can delete
nullptr
without fear of nastiness if you're not sure whether or not the pointer is in use or not.
Upvotes: 1
Reputation: 24946
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.
sizeof(int*)
bytes of memory to store an address of type int
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).
Upvotes: 1
Reputation: 141618
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';
Upvotes: 9
Reputation: 1294
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.
Upvotes: 1
Reputation: 27577
And I think when using
nullptr
initializes a pointer which points to nothing. Is that correct? When would you use one over the other? Why would you initialize a pointer to nullptr if you could just declare it and then initialize it later?
Yes. The important thing to remember is that as soon as you allocate memory to the pointer you have to manage it. So nullptr
is a sentinel value that means the pointer doesn't point to anything.
Is new only used for dynamic memory allocation or are there other applications for it?
That is the only function of new
. You can specialise this behavior with placement new
but you're still allocating memory.
Upvotes: 0
Reputation: 1223
Your understanding is correct. nullptr
is a good value to initialize, to indicate that there is no memory pointed to by the pointer. Also, delete
operation on nullptr
is safe, whereas delete
on arbitrary values (which is typically the case when you don't initialize the pointer) can cause Segmentation faults.
When using new
, if you wish to initialize the memory to zero, you should use new int()
. Otherwise, the memory can have residual data.
Upvotes: 0