tmp/tmpxinn2cgr/{from.md → to.md}
RENAMED
|
@@ -6,10 +6,14 @@ destroyed using *delete-expression*s ([[expr.delete]]). A
|
|
| 6 |
C++implementation provides access to, and management of, dynamic storage
|
| 7 |
via the global *allocation functions* `operator new` and `operator
|
| 8 |
new[]` and the global *deallocation functions* `operator
|
| 9 |
delete` and `operator delete[]`.
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
The library provides default definitions for the global allocation and
|
| 12 |
deallocation functions. Some global allocation and deallocation
|
| 13 |
functions are replaceable ([[new.delete]]). A C++program shall provide
|
| 14 |
at most one definition of a replaceable allocation or deallocation
|
| 15 |
function. Any such function definition replaces the default version
|
|
@@ -18,27 +22,41 @@ allocation and deallocation functions ([[support.dynamic]]) are
|
|
| 18 |
implicitly declared in global scope in each translation unit of a
|
| 19 |
program.
|
| 20 |
|
| 21 |
``` cpp
|
| 22 |
void* operator new(std::size_t);
|
| 23 |
-
void* operator new
|
|
|
|
| 24 |
void operator delete(void*) noexcept;
|
| 25 |
-
void operator delete[](void*) noexcept;
|
| 26 |
void operator delete(void*, std::size_t) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
void operator delete[](void*, std::size_t) noexcept;
|
|
|
|
|
|
|
| 28 |
```
|
| 29 |
|
| 30 |
These implicit declarations introduce only the function names `operator`
|
| 31 |
`new`, `operator` `new[]`, `operator` `delete`, and `operator`
|
| 32 |
-
`delete[]`.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
Any allocation and/or deallocation functions defined in a C++program,
|
| 42 |
including the default versions in the library, shall conform to the
|
| 43 |
semantics specified in [[basic.stc.dynamic.allocation]] and
|
| 44 |
[[basic.stc.dynamic.deallocation]].
|
|
@@ -63,102 +81,91 @@ storage. If it is successful, it shall return the address of the start
|
|
| 63 |
of a block of storage whose length in bytes shall be at least as large
|
| 64 |
as the requested size. There are no constraints on the contents of the
|
| 65 |
allocated storage on return from the allocation function. The order,
|
| 66 |
contiguity, and initial value of storage allocated by successive calls
|
| 67 |
to an allocation function are unspecified. The pointer returned shall be
|
| 68 |
-
suitably aligned so that it can be converted to a pointer
|
| 69 |
-
complete object type
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
An allocation function that fails to allocate storage can invoke the
|
| 81 |
-
currently installed new-handler function ([[new.handler]]), if any.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
A global allocation function is only called as the result of a new
|
| 92 |
expression ([[expr.new]]), or called directly using the function call
|
| 93 |
syntax ([[expr.call]]), or called indirectly through calls to the
|
| 94 |
-
functions in the C++standard library.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
[[
|
|
|
|
|
|
|
| 100 |
|
| 101 |
#### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
|
| 102 |
|
| 103 |
Deallocation functions shall be class member functions or global
|
| 104 |
functions; a program is ill-formed if deallocation functions are
|
| 105 |
declared in a namespace scope other than global scope or declared static
|
| 106 |
in global scope.
|
| 107 |
|
| 108 |
Each deallocation function shall return `void` and its first parameter
|
| 109 |
-
shall be `void*`. A deallocation function
|
| 110 |
-
parameter.
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
parameters, the
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
deallocation function named `operator` `delete[]` with exactly one
|
| 125 |
-
parameter, then that function is a usual (non-placement) deallocation
|
| 126 |
-
function. If class `T` does not declare such an `operator` `delete[]`
|
| 127 |
-
but does declare a member deallocation function named `operator`
|
| 128 |
-
`delete[]` with exactly two parameters, the second of which has type
|
| 129 |
-
`std::size_t`, then this function is a usual deallocation function. A
|
| 130 |
-
deallocation function can be an instance of a function template. Neither
|
| 131 |
-
the first parameter nor the return type shall depend on a template
|
| 132 |
-
parameter. That is, a deallocation function template shall have a first
|
| 133 |
parameter of type `void*` and a return type of `void` (as specified
|
| 134 |
-
above).
|
|
|
|
|
|
|
| 135 |
parameters. A template instance is never a usual deallocation function,
|
| 136 |
regardless of its signature.
|
| 137 |
|
| 138 |
If a deallocation function terminates by throwing an exception, the
|
| 139 |
behavior is undefined. The value of the first argument supplied to a
|
| 140 |
deallocation function may be a null pointer value; if so, and if the
|
| 141 |
deallocation function is one supplied in the standard library, the call
|
| 142 |
-
has no effect.
|
| 143 |
-
supplied to `operator` `delete(void*)` in the standard library is not
|
| 144 |
-
one of the values returned by a previous invocation of either `operator`
|
| 145 |
-
`new(std::size_t)` or `operator` `new(std::size_t,` `const`
|
| 146 |
-
`std::nothrow_t&)` in the standard library, and the behavior is
|
| 147 |
-
undefined if the value supplied to `operator` `delete[](void*)` in the
|
| 148 |
-
standard library is not one of the values returned by a previous
|
| 149 |
-
invocation of either `operator` `new[](std::size_t)` or `operator`
|
| 150 |
-
`new[](std::size_t,` `const` `std::nothrow_t&)` in the standard library.
|
| 151 |
|
| 152 |
If the argument given to a deallocation function in the standard library
|
| 153 |
is a pointer that is not the null pointer value ([[conv.ptr]]), the
|
| 154 |
deallocation function shall deallocate the storage referenced by the
|
| 155 |
-
pointer,
|
| 156 |
-
deallocated storage. Indirection through an invalid pointer value and
|
| 157 |
-
passing an invalid pointer value to a deallocation function have
|
| 158 |
-
undefined behavior. Any other use of an invalid pointer value has
|
| 159 |
-
implementation-defined behavior.[^14]
|
| 160 |
|
| 161 |
#### Safely-derived pointers <a id="basic.stc.dynamic.safety">[[basic.stc.dynamic.safety]]</a>
|
| 162 |
|
| 163 |
A *traceable pointer object* is
|
| 164 |
|
|
@@ -171,11 +178,12 @@ A *traceable pointer object* is
|
|
| 171 |
|
| 172 |
A pointer value is a *safely-derived pointer* to a dynamic object only
|
| 173 |
if it has an object pointer type and it is one of the following:
|
| 174 |
|
| 175 |
- the value returned by a call to the C++standard library implementation
|
| 176 |
-
of `::operator new(std::size_t)`
|
|
|
|
| 177 |
- the result of taking the address of an object (or one of its
|
| 178 |
subobjects) designated by an lvalue resulting from indirection through
|
| 179 |
a safely-derived pointer value;
|
| 180 |
- the result of well-defined pointer arithmetic ([[expr.add]]) using a
|
| 181 |
safely-derived pointer value;
|
|
@@ -186,13 +194,13 @@ if it has an object pointer type and it is one of the following:
|
|
| 186 |
safely-derived pointer value;
|
| 187 |
- the value of an object whose value was copied from a traceable pointer
|
| 188 |
object, where at the time of the copy the source object contained a
|
| 189 |
copy of a safely-derived pointer value.
|
| 190 |
|
| 191 |
-
An integer value is an
|
| 192 |
-
|
| 193 |
-
|
| 194 |
|
| 195 |
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 196 |
- the result of a valid conversion of an integer representation of a
|
| 197 |
safely-derived pointer value;
|
| 198 |
- the value of an object whose value was copied from a traceable pointer
|
|
@@ -209,12 +217,16 @@ validity of a pointer value does not depend on whether it is a
|
|
| 209 |
safely-derived pointer value. Alternatively, an implementation may have
|
| 210 |
*strict pointer safety*, in which case a pointer value referring to an
|
| 211 |
object with dynamic storage duration that is not a safely-derived
|
| 212 |
pointer value is an invalid pointer value unless the referenced complete
|
| 213 |
object has previously been declared reachable (
|
| 214 |
-
[[util.dynamic.safety]]).
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
[[basic.stc.dynamic.deallocation]]. This is true even if the
|
| 217 |
unsafely-derived pointer value might compare equal to some
|
| 218 |
-
safely-derived pointer value.
|
| 219 |
-
|
|
|
|
|
|
|
| 220 |
|
|
|
|
| 6 |
C++implementation provides access to, and management of, dynamic storage
|
| 7 |
via the global *allocation functions* `operator new` and `operator
|
| 8 |
new[]` and the global *deallocation functions* `operator
|
| 9 |
delete` and `operator delete[]`.
|
| 10 |
|
| 11 |
+
[*Note 1*: The non-allocating forms described in
|
| 12 |
+
[[new.delete.placement]] do not perform allocation or
|
| 13 |
+
deallocation. — *end note*]
|
| 14 |
+
|
| 15 |
The library provides default definitions for the global allocation and
|
| 16 |
deallocation functions. Some global allocation and deallocation
|
| 17 |
functions are replaceable ([[new.delete]]). A C++program shall provide
|
| 18 |
at most one definition of a replaceable allocation or deallocation
|
| 19 |
function. Any such function definition replaces the default version
|
|
|
|
| 22 |
implicitly declared in global scope in each translation unit of a
|
| 23 |
program.
|
| 24 |
|
| 25 |
``` cpp
|
| 26 |
void* operator new(std::size_t);
|
| 27 |
+
void* operator new(std::size_t, std::align_val_t);
|
| 28 |
+
|
| 29 |
void operator delete(void*) noexcept;
|
|
|
|
| 30 |
void operator delete(void*, std::size_t) noexcept;
|
| 31 |
+
void operator delete(void*, std::align_val_t) noexcept;
|
| 32 |
+
void operator delete(void*, std::size_t, std::align_val_t) noexcept;
|
| 33 |
+
|
| 34 |
+
void* operator new[](std::size_t);
|
| 35 |
+
void* operator new[](std::size_t, std::align_val_t);
|
| 36 |
+
|
| 37 |
+
void operator delete[](void*) noexcept;
|
| 38 |
void operator delete[](void*, std::size_t) noexcept;
|
| 39 |
+
void operator delete[](void*, std::align_val_t) noexcept;
|
| 40 |
+
void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
|
| 41 |
```
|
| 42 |
|
| 43 |
These implicit declarations introduce only the function names `operator`
|
| 44 |
`new`, `operator` `new[]`, `operator` `delete`, and `operator`
|
| 45 |
+
`delete[]`.
|
| 46 |
+
|
| 47 |
+
[*Note 2*: The implicit declarations do not introduce the names `std`,
|
| 48 |
+
`std::size_t`, `std::align_val_t`, or any other names that the library
|
| 49 |
+
uses to declare these names. Thus, a *new-expression*,
|
| 50 |
+
*delete-expression* or function call that refers to one of these
|
| 51 |
+
functions without including the header `<new>` is well-formed. However,
|
| 52 |
+
referring to `std` or `std::size_t` or `std::align_val_t` is ill-formed
|
| 53 |
+
unless the name has been declared by including the appropriate
|
| 54 |
+
header. — *end note*]
|
| 55 |
+
|
| 56 |
+
Allocation and/or deallocation functions may also be declared and
|
| 57 |
+
defined for any class ([[class.free]]).
|
| 58 |
|
| 59 |
Any allocation and/or deallocation functions defined in a C++program,
|
| 60 |
including the default versions in the library, shall conform to the
|
| 61 |
semantics specified in [[basic.stc.dynamic.allocation]] and
|
| 62 |
[[basic.stc.dynamic.deallocation]].
|
|
|
|
| 81 |
of a block of storage whose length in bytes shall be at least as large
|
| 82 |
as the requested size. There are no constraints on the contents of the
|
| 83 |
allocated storage on return from the allocation function. The order,
|
| 84 |
contiguity, and initial value of storage allocated by successive calls
|
| 85 |
to an allocation function are unspecified. The pointer returned shall be
|
| 86 |
+
suitably aligned so that it can be converted to a pointer to any
|
| 87 |
+
suitable complete object type ([[new.delete.single]]) and then used to
|
| 88 |
+
access the object or array in the storage allocated (until the storage
|
| 89 |
+
is explicitly deallocated by a call to a corresponding deallocation
|
| 90 |
+
function). Even if the size of the space requested is zero, the request
|
| 91 |
+
can fail. If the request succeeds, the value returned shall be a
|
| 92 |
+
non-null pointer value ([[conv.ptr]]) `p0` different from any
|
| 93 |
+
previously returned value `p1`, unless that value `p1` was subsequently
|
| 94 |
+
passed to an `operator` `delete`. Furthermore, for the library
|
| 95 |
+
allocation functions in [[new.delete.single]] and
|
| 96 |
+
[[new.delete.array]], `p0` shall represent the address of a block of
|
| 97 |
+
storage disjoint from the storage for any other object accessible to the
|
| 98 |
+
caller. The effect of indirecting through a pointer returned as a
|
| 99 |
+
request for zero size is undefined.[^13]
|
| 100 |
|
| 101 |
An allocation function that fails to allocate storage can invoke the
|
| 102 |
+
currently installed new-handler function ([[new.handler]]), if any.
|
| 103 |
+
|
| 104 |
+
[*Note 1*: A program-supplied allocation function can obtain the
|
| 105 |
+
address of the currently installed `new_handler` using the
|
| 106 |
+
`std::get_new_handler` function ([[set.new.handler]]). — *end note*]
|
| 107 |
+
|
| 108 |
+
If an allocation function that has a non-throwing exception
|
| 109 |
+
specification ([[except.spec]]) fails to allocate storage, it shall
|
| 110 |
+
return a null pointer. Any other allocation function that fails to
|
| 111 |
+
allocate storage shall indicate failure only by throwing an exception (
|
| 112 |
+
[[except.throw]]) of a type that would match a handler (
|
| 113 |
+
[[except.handle]]) of type `std::bad_alloc` ([[bad.alloc]]).
|
| 114 |
|
| 115 |
A global allocation function is only called as the result of a new
|
| 116 |
expression ([[expr.new]]), or called directly using the function call
|
| 117 |
syntax ([[expr.call]]), or called indirectly through calls to the
|
| 118 |
+
functions in the C++standard library.
|
| 119 |
+
|
| 120 |
+
[*Note 2*: In particular, a global allocation function is not called to
|
| 121 |
+
allocate storage for objects with static storage duration (
|
| 122 |
+
[[basic.stc.static]]), for objects or references with thread storage
|
| 123 |
+
duration ([[basic.stc.thread]]), for objects of type `std::type_info` (
|
| 124 |
+
[[expr.typeid]]), or for an exception object (
|
| 125 |
+
[[except.throw]]). — *end note*]
|
| 126 |
|
| 127 |
#### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
|
| 128 |
|
| 129 |
Deallocation functions shall be class member functions or global
|
| 130 |
functions; a program is ill-formed if deallocation functions are
|
| 131 |
declared in a namespace scope other than global scope or declared static
|
| 132 |
in global scope.
|
| 133 |
|
| 134 |
Each deallocation function shall return `void` and its first parameter
|
| 135 |
+
shall be `void*`. A deallocation function may have more than one
|
| 136 |
+
parameter. A *usual deallocation function* is a deallocation function
|
| 137 |
+
that has:
|
| 138 |
+
|
| 139 |
+
- exactly one parameter; or
|
| 140 |
+
- exactly two parameters, the type of the second being either
|
| 141 |
+
`std::align_val_t` or `std::size_t` [^14]; or
|
| 142 |
+
- exactly three parameters, the type of the second being `std::size_t`
|
| 143 |
+
and the type of the third being `std::align_val_t`.
|
| 144 |
+
|
| 145 |
+
A deallocation function may be an instance of a function template.
|
| 146 |
+
Neither the first parameter nor the return type shall depend on a
|
| 147 |
+
template parameter.
|
| 148 |
+
|
| 149 |
+
[*Note 1*: That is, a deallocation function template shall have a first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
parameter of type `void*` and a return type of `void` (as specified
|
| 151 |
+
above). — *end note*]
|
| 152 |
+
|
| 153 |
+
A deallocation function template shall have two or more function
|
| 154 |
parameters. A template instance is never a usual deallocation function,
|
| 155 |
regardless of its signature.
|
| 156 |
|
| 157 |
If a deallocation function terminates by throwing an exception, the
|
| 158 |
behavior is undefined. The value of the first argument supplied to a
|
| 159 |
deallocation function may be a null pointer value; if so, and if the
|
| 160 |
deallocation function is one supplied in the standard library, the call
|
| 161 |
+
has no effect.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
If the argument given to a deallocation function in the standard library
|
| 164 |
is a pointer that is not the null pointer value ([[conv.ptr]]), the
|
| 165 |
deallocation function shall deallocate the storage referenced by the
|
| 166 |
+
pointer, ending the duration of the region of storage.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
#### Safely-derived pointers <a id="basic.stc.dynamic.safety">[[basic.stc.dynamic.safety]]</a>
|
| 169 |
|
| 170 |
A *traceable pointer object* is
|
| 171 |
|
|
|
|
| 178 |
|
| 179 |
A pointer value is a *safely-derived pointer* to a dynamic object only
|
| 180 |
if it has an object pointer type and it is one of the following:
|
| 181 |
|
| 182 |
- the value returned by a call to the C++standard library implementation
|
| 183 |
+
of `::operator new(std::{}size_t)` or
|
| 184 |
+
`::operator new(std::size_t, std::align_val_t)` ;[^15]
|
| 185 |
- the result of taking the address of an object (or one of its
|
| 186 |
subobjects) designated by an lvalue resulting from indirection through
|
| 187 |
a safely-derived pointer value;
|
| 188 |
- the result of well-defined pointer arithmetic ([[expr.add]]) using a
|
| 189 |
safely-derived pointer value;
|
|
|
|
| 194 |
safely-derived pointer value;
|
| 195 |
- the value of an object whose value was copied from a traceable pointer
|
| 196 |
object, where at the time of the copy the source object contained a
|
| 197 |
copy of a safely-derived pointer value.
|
| 198 |
|
| 199 |
+
An integer value is an *integer representation of a safely-derived
|
| 200 |
+
pointer* only if its type is at least as large as `std::intptr_t` and it
|
| 201 |
+
is one of the following:
|
| 202 |
|
| 203 |
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 204 |
- the result of a valid conversion of an integer representation of a
|
| 205 |
safely-derived pointer value;
|
| 206 |
- the value of an object whose value was copied from a traceable pointer
|
|
|
|
| 217 |
safely-derived pointer value. Alternatively, an implementation may have
|
| 218 |
*strict pointer safety*, in which case a pointer value referring to an
|
| 219 |
object with dynamic storage duration that is not a safely-derived
|
| 220 |
pointer value is an invalid pointer value unless the referenced complete
|
| 221 |
object has previously been declared reachable (
|
| 222 |
+
[[util.dynamic.safety]]).
|
| 223 |
+
|
| 224 |
+
[*Note 1*: The effect of using an invalid pointer value (including
|
| 225 |
+
passing it to a deallocation function) is undefined, see
|
| 226 |
[[basic.stc.dynamic.deallocation]]. This is true even if the
|
| 227 |
unsafely-derived pointer value might compare equal to some
|
| 228 |
+
safely-derived pointer value. — *end note*]
|
| 229 |
+
|
| 230 |
+
It is *implementation-defined* whether an implementation has relaxed or
|
| 231 |
+
strict pointer safety.
|
| 232 |
|