[mythtv] Re: Myth Theme Memory Usage

Alan Gonzalez alandgonzalez at gmail.com
Thu Dec 9 02:42:47 UTC 2004


> 
> Actually, it's not *guaranteed* to be OK to delete a NULL pointer...
> though it usually is, it's implementation-defined behavior.  I usually
> do the following just to be safe:
> 
>         if (ptr)
>         {
>                 delete ptr;
>                 ptr = NULL;
>         }
> 

Really?  The C99 specification states otherwise.   I've been under the
assumption that this is an old myth that continues to be propagated. 
Have you actually seen something like that happen?  I haven't.  I know
that GCC, G++ and Microsoft compilers (look at MSDN docs on 'delete'
operator) check the value of the pointer being sent in.

So putting an if check around deletes is one of those things that
bothers me to see.  Setting the pointer to 0/NULL though is good when
reusing ptrs.

Alan

Found this:

"C++ guarantees that operator delete checks its argument for
null-ness. If the argument is 0, the delete expression has no effect.
In other words, deleting a null pointer is a safe (yet useless)
operation. There is no need to check the pointer for null-ness before
passing it to delete:

 
if (p) // useless; delete already checks for a null value
  delete(p);
"

Alan


More information about the mythtv-dev mailing list