Reputation:
Why can you print the value of a null pointer but not an uninitialized pointer? I mean when initializing a pointer to nullptr, you just explicitly make the pointer point to nowhere, right? If so, isn't that just the same as not initializing it, or is there something that I have missed?
Thanks! Much appreciated!
Upvotes: 7
Views: 2753
Reputation: 6067
I mean when initializing a pointer to nullptr, you just explicitly make the pointer point to nowhere, right? If so, isn't that just the same as not initializing it.
No both are not same!
Reference: C++ Primer
The value (i.e., the address) stored in a pointer can be in one of four states:
When we say a pointer is initialized to nullptr
it means the pointer is not bound to any object.
An uninitialized pointer is in the invalid state (point 4 above).
You can check if a pointer is null, however dereferencing an uninitialized pointer is undefined and may result in a run-time crash.
Upvotes: 2
Reputation: 62169
In some languages like C# and Java an uninitialized pointer contains NULL.
On the other hand, C++ follows the philosophy of not wasting any clock cycles doing things which you have not explicitly asked it to, (how successful the language is at that is a different story,) so C++ does not waste any time initializing pointers to NULL, so in C++ an uninitialized pointer is said to contain garbage.
The problem with garbage is that later you cannot say if( pointer == garbage ) { pointer = someMeaningfulValue; }
because you do not know what the garbage value is so as to compare against it. You cannot reproduce it.
So, by saying pointer = NULL;
you have initialized the pointer with a value which is known in advance, (and stands for "Points To Nothing",) so you can later safely do if( pointer == NULL ) { pointer = someMeaningfulValue; }
.
Upvotes: 2
Reputation: 477680
A pointer (of type T
) is a value, similar to other values like integers or strings, and it can be in one of three states:
It can point to an object or one past an object (and the two are not exclusive), or it points at a function. Such a pointer may be dereferenced if it points at an object or a function.
It can have the special value "null", in which case it compares equal to T()
and to nullptr
and to other expressions like 0
, NULL
, T(0)
and T(nullptr)
.
It can be none of the above. This is true of the value of an uninitialized variable of type T
, but never of a prvalue of T
. This is also true if the pointer once did point to an object but the object's lifetime has ended.
Note that only case (2), the null pointer, is something that you can check for programmatically! You cannot in general tell whether a non-null pointer is valid (short of perhaps comparing a pointer to a set of known, valid pointers).
Upvotes: 3
Reputation: 3305
nullptr
means nowhere, that's OK! and is a good practice initialize pointers to this defined value.
But being uninitialized it can point Anywhere, it is undefined and can lead to undefined behavior
Upvotes: 2
Reputation: 385405
Pointing to nowhere, and being uninitialised, are two different concepts.
Strictly, an uninitialised pointer has an indeterminate value. It's not a pointer to the specific place "nowhere", or to an object. It's just … "shrug". You can't do anything with it yet.
"The specific place 'nowhere'??" I hear you ask. "What is this nonsense?" Well, quite. It's not really "nowhere". A null pointer has a specific value and a specific representation. It doesn't point to an object, but it is still a single, predictable, known value, reserved for this use.
Valid pointer: I'm at Kerrek's house, or at work, or at the pub, etc.
Invalidated pointer: I was at Kerrek's house, but Vlad from Moscow blew it up :( I'm currently watching the flames and crying a little.
Null pointer: I'm asleep at home. Come back in the morning, please.
Uninitialised pointer: I am very drunk and have no earthly idea where I am, if I'm even still alive.
Upvotes: 13