Partager via


fonctions<new>

get_new_handler

new_handler get_new_handler() noexcept;

Notes

Retourne le new_handlerfichier actuel .

Blanchir

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

Paramètres

ptr
Adresse d’un octet en mémoire qui contient un objet dont le type est similaire à T.

Valeur de retour

Valeur de type T* qui pointe vers X.

Notes

Également appelé barrière d’optimisation du pointeur.

Utilisé comme expression constante lorsque la valeur de son argument peut être utilisée dans une expression constante. Un octet de stockage est accessible via une valeur de pointeur qui pointe vers un objet si dans le stockage occupé par un autre objet, un objet avec un pointeur similaire.

Exemple

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

Fournit un objet à utiliser comme argument pour les nothrow versions de new et delete.

extern const std::nothrow_t nothrow;

Notes

L’objet est utilisé comme argument de fonction pour correspondre au type de paramètre std::nothrow_t.

Exemple

Consultez operator new et operator new[] pour obtenir des exemples d’utilisation std::nothrow_t comme paramètre de fonction.

set_new_handler

Installe une fonction utilisateur à appeler lorsque l’opérateur ne parvient pas à allouer de la mémoire.

new_handler set_new_handler(new_handler Pnew) throw();

Paramètres

Pnew
new_handler À installer.

Valeur de retour

0 lors du premier appel et new_handler précédent lors des appels ultérieurs.

Notes

La fonction stocke Pnew dans un pointeur de gestionnaire statique new qu’elle conserve, puis retourne la valeur précédemment stockée dans le pointeur. Le new gestionnaire est utilisé par operator new.

Exemple

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