Pointers open us up to ways to accidentally cause our programs to crash. In particular, we need to look out for
dereferencing invalid addresses.
Remember that if we declare an int without setting a value, it will start with "garbage" as its value:
int number;
cout << number << endl;
32741
The same issue happens with a pointer. If we don't initialize it, it will get "garbage", and when we
display it it will look like a memory address, but it's an invalid one!
int * ptr;
cout << ptr << endl;
0x7f7efe1fc934
If we dereference this invalid memory address, the program will crash or give some kind of
undefined behavior. This is why we initialize our pointers to nullptr
!