Possible Duplicate:
Is there any reason to check for a NULL pointer before deleting?
I often see the following in code:
if(pointer)
delete pointer;
To my understanding it is safe to delete a null pointer, so what is the point of this check?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
deletewill check if the pointer is NULL for you, so you’re right that the check isn’t needed.You might also see that some people set the pointer to NULL after it’s deleted so that you don’t do anything stupid like try and use memory that is no longer yours or stop you from deleting the pointer twice, which will cause an error.