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; } .
source share