Because it is OK to delete a NULL(0)
pointer. The is no need to check if the pointer actually is NULL(0)
or not. If you want to set the pointer to NULL, after deleting, then you can overload the delete
operator globally with out using macros.
It seems that I was wrong about the second point:
If you want to set the pointer to
NULL, after deleting, then you can
overload the delete
operator
globally
The thing is that if you overload the global new
and delete
, you could have something like this:
void* operator new(size_t size)
{
void* ptr = malloc(size);
if(ptr != 0)
{
return ptr;
}
throw std::bad_alloc("Sorry, the allocation didn't go well!");
}
void operator delete(void* p)
{
free(p);
p = 0;
}
Now, if you set p = 0;
in the overloaded delete
, you are actually setting the local
one, but not the original p
. Basically, we are getting a copy of the pointer in the overloaded delete
.
Sorry, it was on the top of my head, I gave it a second thought now. Anyway, I would write template inline function to do the thing instead of writing EVIL MACROS :)