21.6.2 Storage allocation and deallocation [new.delete]
Except where otherwise specified, the provisions of
[basic.stc.dynamic]
apply to the library versions of
operator new and
operator
delete. If the value of an alignment argument
passed to any of these functions
is not a valid alignment value,
the behavior is undefined
.void* operator new(std::size_t size);
void* operator new(std::size_t size, std::align_val_t alignment);
The second form is called for a type with new-extended alignment,
and allocates storage
with the specified alignment
. The first form is called otherwise,
and allocates storage
suitably aligned to represent any object of that size
provided the object's type does not have new-extended alignment
. Replaceable:
A C++ program may define functions with either of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. Required behavior:
Return a non-null pointer to suitably aligned storage (
[basic.stc.dynamic]),
or else throw a
bad_alloc
exception
. This requirement is binding on any replacement versions of these functions
.Default behavior:Executes a loop:
Within the loop, the function first attempts to allocate the requested storage
. Whether the attempt involves a call to the C standard library functions
malloc or
aligned_alloc
is unspecified
. Returns a pointer to the allocated storage if the attempt is successful
. Otherwise, if the
current
new_handler (
[get.new.handler]) is
a null pointer value, throws
bad_alloc.Otherwise, the function calls the current
new_handler function (
[new.handler])
. If the called function returns, the loop repeats
.The loop terminates when an attempt to allocate the requested storage is
successful or when a called
new_handler
function does not return
.
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;
Effects:
Same as above, except that these are called by a placement version of a
new-expression
when a C++ program prefers a null pointer result as an error indication,
instead of a
bad_alloc
exception
. Replaceable:
A C++ program may define functions with either of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. Required behavior:
Return a non-null pointer to suitably aligned storage (
[basic.stc.dynamic]),
or else return a null pointer
. Each of these nothrow versions of
operator new
returns a pointer obtained as if
acquired from the (possibly replaced)
corresponding non-placement function
. This requirement is binding on any replacement versions of these functions
.Default behavior:
Calls
operator new(size),
or
operator new(size, alignment),
respectively
. If the call returns normally,
returns the result of that call
. Otherwise, returns a null pointer
.[
Example:
T* p1 = new T; T* p2 = new(nothrow) T;
—
end example ]
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;
Replaceable:
A C++ program may define functions with any of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. If a function without a
size parameter is defined,
the program should also define
the corresponding function with a
size parameter
. If a function with a
size parameter is defined,
the program shall also define
the corresponding version without the
size parameter
. [
Note: The default behavior below may change in the future, which will require
replacing both deallocation functions when replacing the allocation function
. —
end note ]
Requires:
ptr shall be a null pointer or
its value shall represent the address of
a block of memory allocated by
an earlier call to a (possibly replaced)
operator new(std::size_t)
or
operator new(std::size_t, std::align_val_t)
which has not been invalidated by an intervening call to
operator delete. Requires:
If the
alignment parameter is not present,
ptr shall have been returned by an allocation function
without an
alignment parameter
. If present, the
alignment argument
shall equal the
alignment argument
passed to the allocation function that returned
ptr. If present, the
size argument
shall equal the
size argument
passed to the allocation function that returned
ptr.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
. [
Note: A conforming implementation is for
operator delete(void* ptr, std::size_t size) to simply call
operator delete(ptr). —
end note ]
Default behavior:
The functions that have a
size parameter
forward their other parameters
to the corresponding function without a
size parameter
. [
Note: See the note in the above
Replaceable: paragraph
. —
end note ]
Default behavior:
If
ptr is null, does nothing
. Otherwise, reclaims the
storage allocated by the earlier call to
operator new.Remarks:
It is unspecified under what conditions part or all of such
reclaimed storage will be allocated by subsequent
calls to
operator new
or any of
aligned_alloc,
calloc,
malloc,
or
realloc,
declared in
<cstdlib>. 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;
Effects:
The
deallocation functions (
[basic.stc.dynamic.deallocation])
called by the implementation
to render the value of
ptr invalid
when the constructor invoked from a nothrow
placement version of the
new-expression throws an exception
. Replaceable:
A C++ program may define functions with either of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. Requires:
ptr shall be a null pointer or
its value shall represent the address of
a block of memory allocated by
an earlier call to a (possibly replaced)
operator new(std::size_t)
or
operator new(std::size_t, std::align_val_t)
which has not been invalidated by an intervening call to
operator delete. Requires:
If the
alignment parameter is not present,
ptr shall have been returned by an allocation function
without an
alignment parameter
. If present, the
alignment argument
shall equal the
alignment argument
passed to the allocation function that returned
ptr.Default behavior:
Calls
operator delete(ptr),
or
operator delete(ptr, alignment),
respectively
. void* operator new[](std::size_t size);
void* operator new[](std::size_t size, std::align_val_t alignment);
The second form is called for a type with new-extended alignment,
and allocates storage
with the specified alignment
. The first form is called otherwise,
and allocates storage
suitably aligned to represent any array object of that size or smaller,
provided the object's type does not have new-extended alignment
. Replaceable:
A C++ program may define functions with either of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. Required behavior:
Same as for
the corresponding single-object forms
. This requirement is binding on any replacement versions of these functions
.Default behavior:
Returns
operator new(size),
or
operator new(size, alignment),
respectively
. 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;
Effects:
Same as above, except that these are called by a placement version of a
new-expression
when a C++ program prefers a null pointer result as an error indication,
instead of a
bad_alloc
exception
. Replaceable:
A C++ program may define functions with either of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. Required behavior:
Return a non-null pointer to suitably aligned storage (
[basic.stc.dynamic]),
or else return a null pointer
. Each of these nothrow versions of
operator new[]
returns a pointer obtained as if
acquired from the (possibly replaced)
corresponding non-placement function
. This requirement is binding on any replacement versions of these functions
.Default behavior:
Calls
operator new[](size),
or
operator new[](size, alignment),
respectively
. If the call returns normally,
returns the result of that call
. Otherwise, returns a null pointer
.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;
Replaceable:
A C++ program may define functions with any of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. If a function without a
size parameter is defined,
the program should also define
the corresponding function with a
size parameter
. If a function with a
size parameter is defined,
the program shall also define
the corresponding version without the
size parameter
. [
Note: The default behavior below may change in the future, which will require
replacing both deallocation functions when replacing the allocation function
. —
end note ]
Requires:
ptr shall be a null pointer or
its value shall represent the address of
a block of memory allocated by
an earlier call to a (possibly replaced)
operator new[](std::size_t)
or
operator new[](std::size_t, std::align_val_t)
which has not been invalidated by an intervening call to
operator delete[]. Requires:
If the
alignment parameter is not present,
ptr shall have been returned by an allocation function
without an
alignment parameter
. If present, the
alignment argument
shall equal the
alignment argument
passed to the allocation function that returned
ptr. If present, the
size argument
shall equal the
size argument
passed to the allocation function that returned
ptr.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
. [
Note: A conforming implementation is for
operator delete[](void* ptr, std::size_t size) to simply call
operator delete[](ptr). —
end note ]
Default behavior:
The functions that have a
size parameter
forward their other parameters
to the corresponding function without a
size parameter
. The functions that do not have a
size parameter
forward their parameters
to the corresponding
operator delete (single-object) function
.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;
Effects:
The
deallocation functions (
[basic.stc.dynamic.deallocation])
called by the implementation
to render the value of
ptr invalid
when the constructor invoked from a nothrow
placement version of the array
new-expression throws an exception
. Replaceable:
A C++ program may define functions with either of these function signatures,
and thereby displace the default versions defined by the
C++ standard library
. Requires:
ptr shall be a null pointer or
its value shall represent the address of
a block of memory allocated by
an earlier call to a (possibly replaced)
operator new[](std::size_t)
or
operator new[](std::size_t, std::align_val_t)
which has not been invalidated by an intervening call to
operator delete[]. Requires:
If the
alignment parameter is not present,
ptr shall have been returned by an allocation function
without an
alignment parameter
. If present, the
alignment argument
shall equal the
alignment argument
passed to the allocation function that returned
ptr.Default behavior:
Calls
operator delete[](ptr),
or
operator delete[](ptr, alignment),
respectively
. These functions are reserved; a C++ program may not define functions that displace
the versions in the C++ standard library (
[constraints])
. The provisions of
[basic.stc.dynamic] do not apply to these reserved
placement forms of
operator new and
operator delete. void* operator new(std::size_t size, void* ptr) noexcept;
Remarks:
Intentionally performs no other action
. [
Example: This can be useful for constructing an object at a known address:
void* place = operator new(sizeof(Something));
Something* p = new (place) Something();
—
end example ]
void* operator new[](std::size_t size, void* ptr) noexcept;
Remarks:
Intentionally performs no other action
. void operator delete(void* ptr, void*) noexcept;
Effects:
Intentionally performs no action
. Remarks:
Default function called when any part of the initialization in a
placement
new-expression that invokes the library's
non-array placement operator new
terminates by throwing an exception (
[expr.new])
. void operator delete[](void* ptr, void*) noexcept;
Effects:
Intentionally performs no action
. Remarks:
Default function called when any part of the initialization in a
placement
new-expression that invokes the library's
array placement operator new
terminates by throwing an exception (
[expr.new])
. For purposes of determining the existence of data races, the library versions
of
operator new, user replacement versions of global
operator new,
the C standard library functions
aligned_alloc,
calloc, and
malloc,
the library
versions of
operator delete, user replacement
versions of
operator delete, the C standard library function
free, and the C standard library function
realloc shall not
introduce a data race (
[res.on.data.races])
. Calls to these functions that allocate or deallocate a particular unit
of storage shall occur in a single total order, and each such deallocation call
shall happen before (
[intro.multithread]) the next allocation (if any) in
this order
.