What happens if 'throw' fails to allocate memory for exception object?

后端 未结 4 431
走了就别回头了
走了就别回头了 2020-11-30 01:48

From C++11 standard (15.1.p4):

The memory for the exception object is allocated in an unspecified way, except as noted in 3.7.4.1

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 02:17

    Actualy it is specified that if allocation for the exception object fails, bad_alloc should be thrown and implementation could also call the new handler.

    This is what is actualy specified in the c++ standard section (§3.7.4.1) you site [basic.stc.dynamic.allocation]:

    An allocation function that fails to allocate storage can invoke the currently installed new-handler function (21.6.3.3), if any. [ Note: A program-supplied allocation function can obtain the address of the currently installed new_handler using the std::get_new_handler function (21.6.3.4). — end note ] If an allocation function that has a non-throwing exception specification (18.4) fails to allocate storage, it shall return a null pointer. Any other allocation function that fails to allocate storage shall indicate failure only by throwing an exception (18.1) of a type that would match a handler (18.3) of type std::bad_alloc (21.6.3.1).

    Then this recalled in [except.terminate]

    In some situations exception handling must be abandoned for less subtle error handling techniques. [ Note: These situations are: — (1.1) when the exception handling mechanism, after completing the initialization of the exception object but before activation of a handler for the exception (18.1)*

    So the itanium ABI does not follow the c++ standard specification, since it can block or call terminate if the program fails to allocate memory for the exception object.

提交回复
热议问题