gpt4 book ai didi

c++ - 不匹配删除不再是未定义的行为?

转载 作者:搜寻专家 更新时间:2023-10-31 00:29:41 25 4
gpt4 key购买 nike

我注意到 C++ drafte51a2152不再包括以下措辞:

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;

将不再有未定义的行为,还是我遗漏了什么?

最佳答案

无论如何,该段涉及分配/解除分配功能。不匹配的 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++ - 不匹配删除不再是未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com