NULL check before deleting an object with an overloaded delete

前端 未结 11 1714
野性不改
野性不改 2020-12-18 18:24

This came up as one of the code review comments.

Is it a good idea to check for NULL before calling delete for any object?

I do understand delete operator c

相关标签:
11条回答
  • 2020-12-18 18:26

    delete (T*)0; is valid and does nothing, similarly free(NULL); is also valid and does nothing. If you overload the delete operator, your implementation should carry the same semantics. The standard says how the standard delete will work, but I don't think it says how an overloaded delete should behave. For the sake of consistency with the standard/default behaviour, it should allow (T*)0 as input.

    0 讨论(0)
  • 2020-12-18 18:31

    I would say that that's more a reason to ensure that if you overload operator delete, then you should always have it check for NULL, otherwise you're breaking the semantics.

    0 讨论(0)
  • 2020-12-18 18:31

    Is it a good idea to check for NULL before calling delete for any object?

    No!

    int *p = NULL;
    delete p ; //no effect
    

    The Standard says [Section 5.3.5/2]

    If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.

    Furthermore in Section 18.4.1.1/13

    void operator delete(void* ptr) throw();

    void operator delete(void* ptr, const std::nothrow_t&) throw();

    Default behavior:

    For a null value of ptr, do nothing.

    — Any other value of ptr shall be a value returned earlier by a call to the default operator new, which was not invalidated by an intervening call to operator delete(void*) (17.4.3.7). For such a non-null value of ptr, reclaims storage allocated by the earlier call to the default operator new.

    EDIT :

    James Kanze here says that

    It's still the responisiblity of operator delete (or delete[]) to check; the standard doesn't guarantee that it won't be given a null pointer; the standard requires that it be a no-op if given a null pointer. Or that the implementation is allowed to call it. According to the latest draft, "The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect." I'm not quite sure what the implications of that "is one supplied in the standard library" are meant to be---taken literally, since his function is not one provided by the standard library, the sentence wouldn't seem to apply. But somehow, that doesn't make sense.

    0 讨论(0)
  • 2020-12-18 18:31

    Is it not necesary to check. If anyone overload the pethod, is his responsibility to to whatever with NULL.

    0 讨论(0)
  • 2020-12-18 18:32

    No need to check for NULL prior to deleting. If someone has overloading delete with something that does not behave in a standard way then that's the real problem. No-one should take lightly the task of overloading delete and should always support expected behaviour such as checking for NULL and taking no action.

    However, for what it's worth, you should always remember to assign zero to any pointer that you've just deleted, unless for example you are about to delete the pointer as well:

    void MyObj::reset()
    {
        delete impl_;
        impl_ = 0;    // Needed here - impl_ may be reused / referenced.
    }
    
    MyObj::~MyObj()
    {
        delete impl_; // No need to assign here as impl_ is going out of scope.
    }
    
    0 讨论(0)
  • 2020-12-18 18:36

    From Standard docs, 18.5.1.1.13 under delete,

    Default behavior: If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.

    So, you don't have to check by default..

    0 讨论(0)
提交回复
热议问题