From: https://en.cppreference.com/w/cpp/memory/new/operator_delete:
If the pointer passed to the standard library deallocation function was not obtained from the corresponding standard library allocation function, the behavior is undefined.
Also, for the first definition:
void operator delete ( void* ptr ) throw(); (until C++11)
void operator delete ( void* ptr ) noexcept; (since C++11)
it says:
The behavior of the standard library implementation of this function is undefined unless ptr is a null pointer or is a pointer previously obtained from the standard library implementation of operator new(size_t)
or operator new(size_t, std::nothrow_t)
.
So, this means if you use new
, you must use delete
on that memory, and if you use malloc()
, you must use free()
on that memory, or else the behavior is undefined.
"undefined" doesn't mean the program doesn't work. It means the language definition doesn't specify what shall happen, so it may or may not work, depending on your compiler's implementation. But, you must NOT count on it to work, so it is considered a bug, and should be fixed.
Note about the reference information
https://en.cppreference.com/w/ is a wiki anyone can edit. It is not the official standard, but it is the closest open-source thing we have to it. Another public standards reference source is http://www.cplusplus.com, which is frequently not as up-to-date, but when it has explanations, I have found them to be much better in that they are easier to understand, but sometimes worse in that they are incomplete or don't cover the latest versions of C++ after C++11. So, use both together. If you're looking for a more-pedantic and more-up-to-date explanation, rely more on cppreference.com, and if you're looking for a more beginner-friendly and easier-to-understand explanation, rely more on cplusplus.com inasmuch as its information is complete and up-to-date.