I can across this while reading C++ Primer:
The pointer we pass to deallocate cannot be null; it must point to memory allocated by allocate.
I checked the source of deallocate
and found this:
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type)
{ ::operator delete(__p); }
Ok, so I know there is a distinction between the delete
expression and operator delete()
. The delete
expression can be used on a null pointer. However, this function calls the global operator delete()
function directly. However, I googled this problem and found a blog post here that also states the global operator delete method checks for null pointers as well, like this:
void
operator delete (void* ptr) throw ()
{
if (ptr)
std::free (ptr);
}
Also, ironically, I also found here that calling std::free
on a null pointer has no effect either... So my question is, why is not permitted for __p
to be a null pointer?