Not great style, but yes that works because the if condition is always true.
If the condition were not always true, then you would be trying to delete an uninitialized pointer.
Since the condition is always true, why have the if at all?
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
should delete [] be called on ptr1 or ptr2? maybe delete [] ptr1 is valid here because the object it points to has been created on the heap but in that case and what happens to ptr2 after delete [] ptr1 as the object it's pointing to has been deleted? perhaps an interm step ptr2 = nullptr;?
maybe delete [] ptr1 is valid here because the object it points to has been created on the heap but in that case and what happens to ptr2 after delete [] ptr1 as the object it's pointing to has been deleted?
delete is invoked on the object pointed to by ptr1, which is also being pointed to by ptr2. Once this object is deleted what does ptr2 point to? Sorry I fail to see why ptr2 does not exist when delete[] is invoked on ptr1:
delete is invoked on the object pointed to by ptr1, which is also being pointed to by ptr2.
Incorrect. In the snippets you were responding to, ptr2 does not exist when delete is invoked as opposed to the code in your post where you've removed the inner scope.
Once this object is deleted what does ptr2 point to?
In your modified code, both ptr1 and ptr2 point to invalid objects (after deletion.) (By contrast, only ptr1 ever points to an invalid object in the previous code snippets.)
Sorry I fail to see why ptr2 does not exist when delete[] is invoked on ptr1:
Yes, well... I suppose if you change the code so it does exist, then comments applied to other code are no longer relevant. Try reinserting the scope you removed: