malloc(0) vs operator new(0)

C99
7.20.3 Memory management functions
If the space cannot be allocated, a null pointer is returned. If the size of the space requested is zero, the behavior is implementationdefined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.


Simply said, when allocation fails, the operator new return null or raise an exception, otherwise, if it succeeds, only return a non-null pointer in C++
also refer to "Item 8:  Adhere to convention when writing operator new and operator delete" in book <<Effective C++>>

ISO C++ 2003
18.4.1.1 Single-object forms
void* operator new(std::size_t size) throw(std::bad_alloc);
Required behavior: Return a non-null pointer to suitably aligned storage (3.7.3), or else throw a bad_alloc exception. This requirement is binding on a replacement version of this function.

3.7.3.1 Allocation functions
Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a nonnull pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete.

An allocation function that fails to allocate storage can invoke the currently installed new_handler (18.4.2.2), if any. [Note: A program-supplied allocation function can obtain the address of the currently installed new_handler using the set_new_handler function (18.4.2.3). ] If an allocation function 
declared with an empty exception-specification (15.4), throw(), fails to allocate storage, it shall return a null pointer. Any other allocation function that fails to allocate storage shall only indicate failure by throwing an exception of class std::bad_alloc (18.4.2.1) or a class derived from td::bad_alloc.

原文地址:https://www.cnblogs.com/aoaoblogs/p/1926879.html