CWG Issue 1748 (original) (raw)
This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2025-11-05
1748. Placement new with a null pointer
Section: 7.6.2.8 [expr.new]Status: CD4Submitter: Marc GlisseDate: 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_allocexception (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_allocexception (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.