Jump to content
  • Advertisement
Sign in to follow this  
Zakwayda

Deleting a null pointer

This topic is 5407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just read online somewhere that C++ gaurantees you can safely call delete on a null pointer. Is this true? That would mean that everywhere I've written if (m_verts) delete m_verts; if (m_texcoords) delete m_texcoords; I could just write delete m_verts; delete m_texcoords; Which I would certainly prefer. Of course I wouldn't want to do that if there were *ever* *any* chance that calling delete on a null pointer would lead to undefined behaviour. So, is it safe? Or not? Thanks...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yes, it's in the C++ standard.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Yes, deleting a null pointer will not harm anything.


It could harm something if the class had an overloaded operator delete, only the standard library version is guaranteed to be a no-op.

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
Quote:
Original post by Washu
Yes, deleting a null pointer will not harm anything.


It could harm something if the class had an overloaded operator delete, only the standard library version is guarenteed to be a no-op.

Your overloaded delete should properly handle that case. If it does not, it is malformed.

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
It could harm something if the class had an overloaded operator delete, only the standard library version is guaranteed to be a no-op.


Somebody who doesn't implement a custom delete operator to be a no-op when called on a NULL pointer is incompetent.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Quote:
Original post by elementary
Quote:
Original post by Washu
Yes, deleting a null pointer will not harm anything.


It could harm something if the class had an overloaded operator delete, only the standard library version is guarenteed to be a no-op.

Your overloaded delete should properly handle that case. If it does not, it is malformed.


Thats not true.


Quote:
Somebody who doesn't implement a custom delete operator to be a no-op when called on a NULL pointer is incompetent.


That is true.

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
Quote:
Original post by Washu
Your overloaded delete should properly handle that case. If it does not, it is malformed.

Thats not true.

Yes, it is. Read the standard sometime.

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
Quote:
Original post by Washu
Yes, deleting a null pointer will not harm anything.


It could harm something if the class had an overloaded operator delete, only the standard library version is guaranteed to be a no-op.

If someone breaks the standard by not checking for null in an overloaded delete operator it's a security hole anyway.
So it is still safe to delete a null pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by psykr
Quote:
Original post by elementary
Quote:
Original post by Washu
Your overloaded delete should properly handle that case. If it does not, it is malformed.

Thats not true.

Yes, it is. Read the standard sometime.


I have, have you? Perhaps you could quote the relevant section that states it is malformed for an operator delete that is not part of the standard library to not be a no-op when called on a null pointer.

Didn't think so.

Quote:

The value of the first argument supplied to one of the deallocation functions provided in the STANDARD LIBRARY may be a null pointer value; if so, the call to the deallocation function has no effect.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!