This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-10-26


1748. Placement new with a null pointer

Section: 7.6.2.8  [expr.new]     Status: CD4     Submitter: Marc Glisse     Date: 2013-09-11

[Moved to DR at the November, 2014 meeting.]

According to 7.6.2.8 [expr.new] paragraph 15,

[Note: unless an allocation function is declared with a non-throwing exception-specification (14.5 [except.spec]), it indicates failure to allocate storage by throwing a std::bad_alloc exception (Clause 14 [except], 17.6.4.1 [bad.alloc]); it returns a non-null pointer otherwise. If the allocation function is declared with a non-throwing exception-specification, it returns null to indicate failure to allocate storage and a non-null pointer otherwise. —end note] If the allocation function returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.

This wording applies even to the non-replaceable placement forms defined in 17.6.3.4 [new.delete.placement] that simply return the supplied pointer as the result of the allocation function. Compilers are thus required to check for a null pointer and avoid the initialization if one is used. This test is unnecessary overhead; it should be the user's responsibility to ensure that a null pointer is not used in these forms of placement new, just as for other cases when a pointer is dereferenced.

Proposed resolution (February, 2014):

Change 7.6.2.8 [expr.new] paragraph 15 as follows:

[Note: unless an allocation function is declared with a non-throwing exception-specification (14.5 [except.spec]), it indicates failure to allocate storage by throwing a std::bad_alloc exception (Clause 14 [except], 17.6.4.1 [bad.alloc]); it returns a non-null pointer otherwise. If the allocation function is declared with a non-throwing exception-specification, it returns null to indicate failure to allocate storage and a non-null pointer otherwise. —end note] If the allocation function is a reserved placement allocation function (17.6.3.4 [new.delete.placement]) that returns null, the behavior is undefined. Otherwise, if the allocation function returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.