CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 21 Jul 2025 23:03:00 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/new/operator new/
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 21 Jul 2025 23:03:01 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"7d4c-ot9cyu+9Zv4sg0RPTom5phBKuOc"
Content-Encoding: gzip
Default allocation functions (single-object form).
The default allocation and deallocation functions are special components of the standard library; They have the following unique properties:
If set_new_handler has been used to define a new_handler function, this new-handler function is called by the default definitions of the allocating versions ((1) and (2)) if they fail to allocate the requested storage.
For the third version, ptr is returned.
Possible output:
Calls to allocation and deallocation functions that reuse the same unit of storage shall occur in a single total order where each deallocation happens entirely before the next allocation.
This shall also apply to the observable behavior of custom replacements for this function.
Otherwise, it throws no exceptions (no-throw guarantee).
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<new>
functions (global namespace)
functions (std namespace)
types
constants
function
<new>
operator new
throwing (1) | void* operator new (std::size_t size) throw (std::bad_alloc); |
---|---|
nothrow (2) | void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw(); |
placement (3) | void* operator new (std::size_t size, void* ptr) throw(); |
throwing (1) | void* operator new (std::size_t size); |
---|---|
nothrow (2) | void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept; |
placement (3) | void* operator new (std::size_t size, void* ptr) noexcept; |
Allocate storage space
- (1) throwing allocation
-
Allocates size bytes of storage, suitably aligned to represent any object of that size, and returns a non-null pointer to the first byte of this block.
On failure, it throws a bad_alloc exception.
- (2) nothrow allocation
-
Same as above (1), except that on failure it returns a null pointer instead of throwing an exception.
If replaced, both the first and second versions shall return pointers with identical properties.
The default definition allocates memory by calling the the first version:::operator new (size)
.
If replaced, both the first and second versions shall return pointers with identical properties.
- (3) placement
-
Simply returns ptr (no storage is allocated).
Notice though that, if the function is called by a new-expression, the proper initialization will be performed (for class objects, this includes calling its default constructor).
The default allocation and deallocation functions are special components of the standard library; They have the following unique properties:
- Global: All three versions of
operator new
are declared in the global namespace, not within the std namespace. - Implicit: The allocating versions ((1) and (2)) are implicitly declared in every translation unit of a C++ program, no matter whether header
<new>
is included or not. - Replaceable: The allocating versions ((1) and (2)) are also replaceable: A program may provide its own definition that replaces the one provided by default to produce the result described above, or can overload it for specific types.
If set_new_handler has been used to define a new_handler function, this new-handler function is called by the default definitions of the allocating versions ((1) and (2)) if they fail to allocate the requested storage.
operator new
can be called explicitly as a regular function, but in C++, new
is an operator with a very specific behavior: An expression with the new
operator, first calls function operator new
(i.e., this function) with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type.Parameters
- size
- Size in bytes of the requested memory block.
This is the size of the type specifier in the new-expression when called automatically by such an expression.
If this argument is zero, the function still returns a distinct non-null pointer on success (although dereferencing this pointer leads to undefined behavior).
size_t is an integral type. - nothrow_value
- The constant nothrow.
This parameter is only used to distinguish it from the first version with an overloaded version. When the nothrow constant is passed as second parameter tooperator new
,operator new
returns a null-pointer on failure instead of throwing a bad_alloc exception.
nothrow_t is the type of constant nothrow.
- ptr
- A pointer to an already-allocated memory block of the proper size.
If called by a new-expression, the object is initialized (or constructed) at this location.
Return value
For the first and second versions, a pointer to the newly allocated storage space.For the third version, ptr is returned.
Example
|
|
Possible output:
1: constructed [0x8f0f70] 2: constructed [0x8f23a8] 3: constructed [0x8f23a8] 4: |
Data races
Modifies the storage referenced by the returned value.Calls to allocation and deallocation functions that reuse the same unit of storage shall occur in a single total order where each deallocation happens entirely before the next allocation.
This shall also apply to the observable behavior of custom replacements for this function.
Exception safety
The first version (1) throws bad_alloc if it fails to allocate storage.Otherwise, it throws no exceptions (no-throw guarantee).
See also
- operator new[]
- Allocate storage space for array (function)
- operator delete
- Deallocate storage space (function)
- nothrow
- Nothrow constant (constant)
- bad_alloc
- Exception thrown on failure allocating memory (class)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us