zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    只要有梦想
    过去这一年
    Importing BizTalk Applications to Production Environment
    SQL Server 2005 – Database Master Key
    Check Page Rank of any web site pages instantly
    An Introduction to SQL Server Service Broker [WORD DOCUMENT]
    Svcutil.exe – Generate the proxy class for the WCF client application
    SQL Server 2005: how to add a linked server
    BizTalk Error: The published message could not be routed
    Enable routing for failed messages in BizTalk 2006
  • 原文地址:https://www.cnblogs.com/aoaoblogs/p/1926879.html
Copyright © 2011-2022 走看看