No, it is undefined to pass any address to delete
which was not returned by new
.
Here is the quote from the Standard.
§ 3.7.4.2-3
If a deallocation function terminates by throwing an exception, the behavior is undefined. 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. Otherwise, the value supplied
to operator delete(void*)
in the standard library shall be 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 value supplied to operator delete[](void*)
in the standard library shall be 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…