Difference between null pointers and uninitialized pointers?

Why can you print a null pointer value but not an uninitialized pointer? I mean, when you initialize a pointer to nullptr, you simply explicitly point the pointer point to nowhere, right? If so, is this not the same as not initializing it, or is there something I missed?

Thanks! Really appreciate!

+5
source share
5 answers

Pointing to nowhere and being uninitialized, there are two different concepts.

Strictly speaking, an uninitialized pointer has an undefined value. This is not a pointer to a specific place "anywhere" or to an object. It is just & hellip; "Shrug". You can’t do anything about it yet.

"A particular place is nowhere?" I hear you asking. "What the hell is this?" Well, quite. This is not "anywhere." A null pointer has a specific meaning and a specific representation. It does not point to an object, but it is still the only, predictable, known value reserved for this use.

Valid signpost: I am in Kerrek’s house or at work, or in a pub, etc.
Invalid pointer: I was in Kerrek’s house, but Vlad from Moscow blew it up: (I am now watching the flames and crying a little.
Zero pointer: I sleep at home. Come back in the morning, please. Uninitialized pointer: I am very drunk and have no earthly idea where I am if I am still alive.

+7
source

A pointer (of type T ) is a value similar to other values, such as integers or strings, and can be in one of three states:

  • It can point to an object or one after the object (and two are not exclusive), or it points to a function. Such a pointer may be dereferenced if it points to an object or function.

  • It may have the special value "null", in which case it is compared with T() and nullptr and with other expressions such as 0 , NULL , T(0) and T(nullptr) .

  • None of this can be. This refers to the value of an uninitialized variable of type T , but never refers to the value of T This is also true if the pointer once pointed to an object, but the object has expired.

Please note that only case (2), a null pointer is something you can check programmatically! You cannot say at all whether a non-zero pointer is valid (unless you compare the pointer with a set of known valid pointers).

+3
source

I mean when initializing a pointer to nullptr, you just explicitly point the pointer point to nowhere, right? If so, it is not like not initializing it.

Both are not the same!

Link: C ++ Primer

A value (i.e. an address) stored in a pointer can be in one of four states:

  • It can point to an object.
  • It can indicate the location immediately beyond the end of the object.
  • It can be a null pointer indicating that it is not tied to any object.
  • It may be invalid; values ​​other than the previous three are not valid.

When we say that the pointer is initialized to nullptr , this means that the pointer is not bound to any object.

An uninitialized pointer is in an invalid state (paragraph 4 above).

You can check if the pointer is null, however the dereferencing of an uninitialized pointer is undefined and may cause a crash at runtime.

+2
source

nullptr doesn't mean anything ok! and it’s good practice to initialize pointers to this particular value. But being uninitialized, it can indicate Anywhere, it is undefined and can lead to undefined behavior

+2
source

In some languages, such as C # and Java, an uninitialized pointer contains NULL.

C ++, on the other hand, follows a philosophy, without spending hours doing things that you clearly didn’t ask for (how successful this language is, this is a different story), so C ++ does not waste time initializing NULL pointers therefore in C ++ an uninitialized pointer contains garbage.

The problem with garbage is that later you cannot say if( pointer == garbage ) { pointer = someMeaningfulValue; } if( pointer == garbage ) { pointer = someMeaningfulValue; } because you don’t know what the meaning of garbage is to compare with it. You cannot play it.

So, saying pointer = NULL; , you initialized a pointer with a value that is known in advance (and means "Points to nobody"), so you can safely do if( pointer == NULL ) { pointer = someMeaningfulValue; } if( pointer == NULL ) { pointer = someMeaningfulValue; } .

+2
source

All Articles