Although your example is trivial, the compiler cannot know (at compile time) the value of the pointer.
At compile time, you can also dereference zero:
// this code compiles Object* pObject = 0; pObject->SomeMethod();
Compilers are not designed to handle these types of errors at compile time.
And in most (all?) Implementations there is a "delete 0" as inoperative. This code should work fine:
Object* pObject = new Object(); delete pObject; pObject = 0; delete pObject;
Although I'm not 100% sure :)
Josh
source share