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.

  • 相关阅读:
    Delphi IDE之向代码中增加方法和字段
    都让Google做了我们还做什么?(WebMap方向)
    Garmin报警点完善计划
    Google Maps API离线开发包(没有网络也可以开发Gmap了)
    试用Google Earth 5中文版(GPS方面)
    多点删除规则和实现方法探讨
    网站备案,烦不甚烦
    分享:CSS菜单和Tab导航选项卡
    Google Latitude试用和思考(LBS之个人监控市场的兴起)
    分享两篇Google Map API的介绍
  • 原文地址:https://www.cnblogs.com/aoaoblogs/p/1926879.html
Copyright © 2011-2022 走看看