Share via


<new> funzioni

get_new_handler

new_handler get_new_handler() noexcept;

Osservazioni:

Restituisce l'oggetto corrente new_handler.

Riciclare

template <class T>
    constexpr T* launder(T* ptr) noexcept;

Parametri

ptr
Indirizzo di un byte in memoria che contiene un oggetto il cui tipo è simile a T.

Valore restituito

Valore di tipo T* che punta a X.

Osservazioni:

Detto anche barriera di ottimizzazione del puntatore.

Utilizzato come espressione costante quando il valore del relativo argomento può essere usato in un'espressione costante. Un byte di archiviazione è raggiungibile tramite un valore del puntatore che punta a un oggetto se all'interno dello spazio di archiviazione occupato da un altro oggetto, un oggetto con un puntatore simile.

Esempio

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

nothrow

Fornisce un oggetto da utilizzare come argomento per le nothrow versioni di new e delete.

extern const std::nothrow_t nothrow;

Osservazioni:

L'oggetto viene usato come argomento di funzione in modo da stabilire una corrispondenza con il tipo di parametro std::nothrow_t.

Esempio

Vedere operator new e operator new[] per esempi di come std::nothrow_t viene usato come parametro di funzione.

set_new_handler

Installa una funzione utente che deve essere chiamata quando l'operatore new non riesce nel tentativo di allocare memoria.

new_handler set_new_handler(new_handler Pnew) throw();

Parametri

Pnew
Oggetto new_handler da installare.

Valore restituito

0 nella prima chiamata e oggetto new_handler precedente nelle chiamate successive.

Osservazioni:

La funzione archivia Pnew in un puntatore del gestore statico new gestito, quindi restituisce il valore archiviato in precedenza nel puntatore. Il new gestore viene usato da operator new.

Esempio

// 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