The first example's problem is that the pointer points to an object whose storage was not allocated via new. Therefore, using delete on it results in undefined behaviour, one symptom of which is a crash. When you set y to be a null pointer, this problem disappears because using delete on a null pointer is safe and results in no net effect.Originally Posted by A36
The second example does not have the problem because the pointer points to an object whose storage was allocated via new, hence using delete is appropriate.