[fdo-dev] Checking new'ed pointers for NULL

Mateusz Loskot mateusz at loskot.net
Mon Aug 28 12:24:17 EDT 2006


Mateusz Loskot wrote:
> Greg Boone wrote:
>> Why do you advocate not to test pointers for NULL before calling delete
>> operator?
> 
> Because of:
> - C++ Standard/ 5.3.5 Delete / 2
> - delete does nothing if its operand == 0, in other words when called
> with 0 (null pointer), it has no effect
> - C++ language guarantees delete p will do nothing if p is null-pointer
> - testing for NULL is redundant and forces reader to analyze 2 x more
> code than it's necessary
> - it does not generate redundant CPU instructions, what may introduce
> better performance when deallocating elements of big collection
> - it may save programmer's time and keyboard ,-)

I've forgot about one important reason:

- delete has no effect on null pointers because of compatibility
with free() from C Standard (1999), that says (7.20.3.2 The free
function): "If ptr is a null pointer, no action occurs."

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net




More information about the Fdo-internals mailing list