不匹配删除不再是未定义的行为?
Mismatched delete no longer undefined behavior?
我注意到 C++ draft as of e51a2152 不再包含以下措辞:
the behavior is undefined if the value supplied to operator delete(void*)
in the standard library is not one of the values returned by a previous invocation of either operator new(std::size_t)
or operator new(std::size_t, const std::nothrow_t&)
in the standard library, and the behavior is undefined if the value supplied to operator delete[](void*)
in the standard library is not one of the values returned by a previous invocation of either operator new[](std::size_t)
or operator new[](std::size_t, const std::nothrow_t&)
in the standard library.
这是否意味着像
这样的代码
int * const p = new int[42];
delete p; // instead of delete[] p;
将不再有未定义的行为,还是我遗漏了什么?
operator delete
:
的措辞已简单地移动到描述中
[new.delete.single]/12:
Requires: ptr
shall be a null pointer or its value shall represent the address of a block of memory allocated by an earlier call to a (possibly replaced) operator new(std::size_t)
or operator new(std::size_t, std::align_val_t)
which has not been invalidated by an intervening call to operator delete
.
有关 GitHub 存储库中的更改,请参阅 here。数组版本也有类似的措辞。语义上没有任何变化,只是它在标准中的表达方式。
无论如何,该段涉及 allocation/deallocation 功能。不匹配的 new
/delete
表达式在 [expr.delete]/2 中处理,它保持不变:
In the first alternative (delete object), the value of the operand of
delete may be a null pointer value, a pointer to a non-array object
created by a previous new-expression, or a pointer to a subobject
([intro.object]) representing a base class of such an object (Clause
[class.derived]). If not, the behavior is undefined. In the second
alternative (delete array), the value of the operand of delete may be
a null pointer value or a pointer value that resulted from a previous
array new-expression.82 If not, the behavior is undefined.
我注意到 C++ draft as of e51a2152 不再包含以下措辞:
the behavior is undefined if the value supplied to
operator delete(void*)
in the standard library is not one of the values returned by a previous invocation of eitheroperator new(std::size_t)
oroperator new(std::size_t, const std::nothrow_t&)
in the standard library, and the behavior is undefined if the value supplied tooperator delete[](void*)
in the standard library is not one of the values returned by a previous invocation of eitheroperator new[](std::size_t)
oroperator new[](std::size_t, const std::nothrow_t&)
in the standard library.
这是否意味着像
这样的代码int * const p = new int[42];
delete p; // instead of delete[] p;
将不再有未定义的行为,还是我遗漏了什么?
operator delete
:
[new.delete.single]/12:
Requires:ptr
shall be a null pointer or its value shall represent the address of a block of memory allocated by an earlier call to a (possibly replaced)operator new(std::size_t)
oroperator new(std::size_t, std::align_val_t)
which has not been invalidated by an intervening call tooperator delete
.
有关 GitHub 存储库中的更改,请参阅 here。数组版本也有类似的措辞。语义上没有任何变化,只是它在标准中的表达方式。
无论如何,该段涉及 allocation/deallocation 功能。不匹配的 new
/delete
表达式在 [expr.delete]/2 中处理,它保持不变:
In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject ([intro.object]) representing a base class of such an object (Clause [class.derived]). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete may be a null pointer value or a pointer value that resulted from a previous array new-expression.82 If not, the behavior is undefined.