What C++17 standard say about calling delete on nullptr?

前端 未结 2 586
礼貌的吻别
礼貌的吻别 2021-01-21 14:36

C++03 Standard say\'s:

5.3.5 Delete

[...] In either alternative, if the value of the operand of delete is the null pointer the operat

相关标签:
2条回答
  • 2021-01-21 15:27

    For destructors, [expr.delete]/6:

    If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted.

    This technically doesn't say that if the operand is a null pointer value, the destructor isn't invoked. Probably a minor wording issue?

    For deallocation, [expr.delete]/7:

    If the value of the operand of the delete-expression is a null pointer value, it is unspecified whether a deallocation function will be called as described above.

    Unspecified deallocation, but likely no destruction.

    Note also, from [basic.stc.dynamic.deallocation]/3, which clarifies that even if the standard library deallocation function is called in this situation, there is no effect:

    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.

    0 讨论(0)
  • 2021-01-21 15:38

    Yes it is valid, and it results in a noop. reference

    If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called.

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