jotik jotik - 1 month ago 18
C++ Question

Mismatched delete no longer undefined behavior?

I noticed that the C++ draft as of e51a2152 no longer includes the following wording:


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.


Does this mean that code like

int * const p = new int[42];
delete p; // instead of delete[] p;


will no longer have undefined behavior, or am I missing something?

Answer

In any event, that paragraph deals with allocation/deallocation functions. Mismatched new/delete expressions are handled in [expr.delete]/2, which remains intact:

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.