Code: Select all
// If p is not pointing to anything.
if ( p == nullptr )
{
// We don't need to process p since it points to nothing.
return;
}
Code: Select all
auto obj = Object.search_by_name ( "placeholder" );
// If we found obj.
if ( obj != nullptr )
{
// Process obj.
}
Simply put, the value of nullptr is null. As in no value! The value is nonexistence itself! It's the difference between being at position [0, 0] and having no position at all! What's the position of 'orgin'? [0, 0]. What's the position of the colour red? Null. As in no value. Because it simply does not make any sense for colours to have a position!
Usually though, this distinction doesn't become apparent and won't cause any problems. Like I said, nullptr is 0 for most systems. This is because pointer value 0 is unusable on most systems so they use it to represent null. However, that's not always the case. It could very well be the complete opposite. Instead of 0, it is actually the biggest value pointer types can represent. Standard loops would normally look like this:
Code: Select all
for ( auto index = 0; index < max; ++index )
{
// Do something with i
}
This distinction is also crucial at understanding other types that have null values. std::string::find() searches for a substring inside another string and returns the substring's position in that string. If it can't find the substring in the string, it returns a null value called std::string::npos. If you have this concept where null == 0, you would think that if find() returns a 0 value, it means that it didn't find anything and is returning null. That's not the case at all. find() returning 0 would just mean that the substring it found is at the 0th location. So what is the value of npos if it is not 0? Simple: it is null; nonexistence. That's all you ever need to know to use it.
Future hardware may even give us the ability to directly represent the concept of 'null' so we don't have to use an actual value to represent it. By enforcing the concept that null == null and not null == 0 or even null = maximum value, we can prepare ourselves for this possible future saving us the trouble of refactoring our code later down the line. Regardless, I hope this convinces you to start learning this concept before it leads to bugs or confusion in the code.
P.S. All pointer types in C++, when zero initialised, will be set to nullptr even if nullptr is not actually 0.
Code: Select all
int* p { }; // Zero initialised pointer. A misnomer since it won't always initialise the pointer to 0 but rather nullptr!