[new.delete.single]

# 17 Language support library [[support]](./#support)

## 17.6 Dynamic memory management [[support.dynamic]](support.dynamic#new.delete.single)

### 17.6.3 Storage allocation and deallocation [[new.delete]](new.delete#single)

#### 17.6.3.2 Single-object forms [new.delete.single]

[ð](#lib:new,operator)

`void* operator new(std::size_t size);
void* operator new(std::size_t size, std::align_val_t alignment);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2440)

*Effects*: The[allocation functions](basic.stc.dynamic.allocation "6.8.6.5.2 Allocation functions [basic.stc.dynamic.allocation]") called by a[*new-expression*](expr.new#nt:new-expression "7.6.2.8 New [expr.new]") ([[expr.new]](expr.new "7.6.2.8 New"))
to allocatesize bytes of storage[.](#1.sentence-1)

The second form is called for a type with new-extended alignment, and
the first form is called otherwise[.](#1.sentence-2)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2451)

*Required behavior*: Return a non-null pointer to suitably aligned storage ([[basic.stc.dynamic]](basic.stc.dynamic "6.8.6.5 Dynamic storage duration")),
or else throw abad_allocexception[.](#2.sentence-1)

This requirement is binding on any replacement versions of these functions[.](#2.sentence-2)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2460)

*Default behavior*: 

- [(3.1)](#3.1)

  Executes a loop:
Within the loop, the function first attempts to allocate the requested storage[.](#3.1.sentence-1)
  Whether the attempt involves a call to the C standard library functionsmalloc or aligned_alloc is unspecified[.](#3.1.sentence-2)

- [(3.2)](#3.2)

  Returns a pointer to the allocated storage if the attempt is successful[.](#3.2.sentence-1)
  Otherwise, if the
current new_handler ([[get.new.handler]](get.new.handler "17.6.4.5 get_­new_­handler")) is
a null pointer value, throwsbad_alloc[.](#3.2.sentence-2)

- [(3.3)](#3.3)

  Otherwise, the function calls the current[new_handler](new.handler "17.6.4.3 Type new_­handler [new.handler]") function[.](#3.3.sentence-1)
  If the called function returns, the loop repeats[.](#3.3.sentence-2)

- [(3.4)](#3.4)

  The loop terminates when an attempt to allocate the requested storage is
successful or when a callednew_handler function does not return[.](#3.4.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2487)

*Remarks*: This function is replaceable ([[dcl.fct.def.replace]](dcl.fct.def.replace#term.replaceable.function "9.6.5 Replaceable function definitions"))[.](#4.sentence-1)

[ð](#lib:new,operator_)

`void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
void* operator new(std::size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept;
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2499)

*Effects*: Same as above, except that these are called by a placement version of a[*new-expression*](expr.new#nt:new-expression "7.6.2.8 New [expr.new]") when a C++ program prefers a null pointer result as an error indication,
instead of abad_alloc exception[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2508)

*Required behavior*: Return a non-null pointer to suitably aligned storage ([[basic.stc.dynamic]](basic.stc.dynamic "6.8.6.5 Dynamic storage duration")),
or else return a null pointer[.](#6.sentence-1)

Each of these nothrow versions ofoperator new returns a pointer obtained as if
acquired from the (possibly replaced)
corresponding non-placement function[.](#6.sentence-2)

This requirement is binding on any replacement versions of these functions[.](#6.sentence-3)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2519)

*Default behavior*: Calls operator new(size),
or operator new(size, alignment),
respectively[.](#7.sentence-1)

If the call returns normally,
returns the result of that call[.](#7.sentence-2)

Otherwise, returns a null pointer[.](#7.sentence-3)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2528)

[*Example [1](#example-1)*: T* p1 = new T; // throws bad_alloc if it fails T* p2 = new(nothrow) T; // returns nullptr if it fails â *end example*]

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2536)

*Remarks*: This function is replaceable ([[dcl.fct.def.replace]](dcl.fct.def.replace#term.replaceable.function "9.6.5 Replaceable function definitions"))[.](#9.sentence-1)

[ð](#lib:delete,operator)

`void operator delete(void* ptr) noexcept;
void operator delete(void* ptr, std::size_t size) noexcept;
void operator delete(void* ptr, std::align_val_t alignment) noexcept;
void operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
`

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2550)

*Preconditions*: ptr is a null pointer or
its value represents the address of
a block of memory allocated by
an earlier call to a (possibly replaced)operator new(std​::​size_t) oroperator new(std​::​size_t, std​::​align_val_t) which has not been invalidated by an intervening call tooperator delete[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2562)

If the alignment parameter is not present,ptr was returned by an allocation function
without an alignment parameter[.](#11.sentence-1)

If present, the alignment argument
is equal to the alignment argument
passed to the allocation function that returned ptr[.](#11.sentence-2)

If present, the size argument
is equal to the size argument
passed to the allocation function that returned ptr[.](#11.sentence-3)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2573)

*Effects*: The
deallocation functions ([[basic.stc.dynamic.deallocation]](basic.stc.dynamic.deallocation "6.8.6.5.3 Deallocation functions"))
called by a[*delete-expression*](expr.delete#nt:delete-expression "7.6.2.9 Delete [expr.delete]") ([[expr.delete]](expr.delete "7.6.2.9 Delete"))
to render the value of ptr invalid[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2581)

*Required behavior*: A call to an operator delete with a size parameter
may be changed to
a call to the corresponding operator delete without a size parameter,
without affecting memory allocation[.](#13.sentence-1)

[*Note [1](#note-1)*:

A conforming implementation is foroperator delete(void* ptr, std​::​size_t size) to simply calloperator delete(ptr)[.](#13.sentence-2)

â *end note*]

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2595)

*Default behavior*: The functions that have a size parameter
forward their other parameters
to the corresponding function without a size parameter[.](#14.sentence-1)

[*Note [2](#note-2)*:

See the note in the below *Remarks*: paragraph[.](#14.sentence-2)

â *end note*]

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2604)

*Default behavior*: If ptr is null, does nothing[.](#15.sentence-1)

Otherwise, reclaims the
storage allocated by the earlier call to operator new[.](#15.sentence-2)

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2609)

*Remarks*: It is unspecified under what conditions part or all of suchreclaimed storage will be allocated by subsequent
calls tooperator new or any ofaligned_alloc,calloc,malloc,
orrealloc,
declared in [<cstdlib>](cstdlib.syn#header:%3ccstdlib%3e "17.2.2 Header <cstdlib> synopsis [cstdlib.syn]")[.](#16.sentence-1)

This function is replaceable ([[dcl.fct.def.replace]](dcl.fct.def.replace#term.replaceable.function "9.6.5 Replaceable function definitions"))[.](#16.sentence-2)

If a replacement function
without a size parameter
is defined by the program,
the program should also define the corresponding
function with a size parameter[.](#16.sentence-3)

If a replacement function
with a size parameter
is defined by the program,
the program shall also define the corresponding
version without the size parameter[.](#16.sentence-4)

[*Note [3](#note-3)*:

The default behavior above might change in the future,
which will require replacing both deallocation functions
when replacing the allocation function[.](#16.sentence-5)

â *end note*]

[ð](#lib:delete,operator_)

`void operator delete(void* ptr, const std::nothrow_t&) noexcept;
void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
`

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2648)

*Preconditions*: ptr is a null pointer or
its value represents the address of
a block of memory allocated by
an earlier call to a (possibly replaced)operator new(std​::​size_t) oroperator new(std​::​size_t, std​::​align_val_t) which has not been invalidated by an intervening call tooperator delete[.](#17.sentence-1)

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2660)

If the alignment parameter is not present,ptr was returned by an allocation function
without an alignment parameter[.](#18.sentence-1)

If present, the alignment argument
is equal to the alignment argument
passed to the allocation function that returned ptr[.](#18.sentence-2)

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2668)

*Effects*: The
deallocation functions ([[basic.stc.dynamic.deallocation]](basic.stc.dynamic.deallocation "6.8.6.5.3 Deallocation functions"))
called by the implementation
to render the value of ptr invalid
when the constructor invoked from a nothrow
placement version of the [*new-expression*](expr.new#nt:new-expression "7.6.2.8 New [expr.new]") throws an exception[.](#19.sentence-1)

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2677)

*Default behavior*: Calls operator delete(ptr),
or operator delete(ptr, alignment),
respectively[.](#20.sentence-1)

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L2683)

*Remarks*: This function is replaceable ([[dcl.fct.def.replace]](dcl.fct.def.replace#term.replaceable.function "9.6.5 Replaceable function definitions"))[.](#21.sentence-1)
