description | title | ms.date | f1_keywords | ms.assetid | |||
---|---|---|---|---|---|---|---|
Learn more about: <new> functions |
<new> functions |
11/04/2016 |
|
e250f06a-b025-4509-ae7a-5356d56aad7d |
new_handler get_new_handler() noexcept;
Returns the current new_handler
.
template <class T>
constexpr T* launder(T* ptr) noexcept;
ptr
The address of a byte in memory that holds an object whose type is similar to T.
A value of type T* that points to X.
Also referred to as a pointer optimization barrier.
Used as a constant expression when the value of its argument may be used in a constant expression. A byte of storage is reachable through a pointer value that points to an object if within the storage occupied by another object, an object with a similar pointer.
struct X { const int n; };
X *p = new X{3};
const int a = p->n;
new (p) X{5}; // p does not point to new object because X::n is const
const int b = p->n; // undefined behavior
const int c = std::launder(p)->n; // OK
Provides an object to be used as an argument for the nothrow
versions of new
and delete
.
extern const std::nothrow_t nothrow;
The object is used as a function argument to match the parameter type std::nothrow_t.
See operator new
and operator new[]
for examples of how std::nothrow_t
is used as a function parameter.
Installs a user function that is to be called when operator new fails in its attempt to allocate memory.
new_handler set_new_handler(new_handler Pnew) throw();
Pnew
The new_handler
to be installed.
0 on the first call and the previous new_handler
on subsequent calls.
The function stores Pnew
in a static new
handler pointer that it maintains, then returns the value previously stored in the pointer. The new
handler is used by operator new
.
// new_set_new_handler.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;
void __cdecl newhandler( )
{
cout << "The new_handler is called:" << endl;
throw bad_alloc( );
return;
}
int main( )
{
set_new_handler (newhandler);
try
{
while ( 1 )
{
new int[5000000];
cout << "Allocating 5000000 ints." << endl;
}
}
catch ( exception e )
{
cout << e.what( ) << endl;
}
}
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
The new_handler is called:
bad allocation