C++ Tutorial/Development/set new handler

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Demonstrating set_new_handler

<source lang="cpp">#include <iostream>

  1. include <new>
  2. include <cstdlib>

using std::cerr; using std::cout; using std::set_new_handler; using std::abort; void f() {

  cerr << "called";
  abort();

} int main() {

  double *ptr[ 50 ];
  set_new_handler( f );
  for ( int i = 0; i < 50; i++ )
  {
     ptr[ i ] = new double[ 50000000 ];
     cout << "Allocated 50000000 doubles in ptr[ " << i << " ]\n";
  }
  return 0;

}</source>

Allocated 50000000 doubles in ptr[ 0 ]
Allocated 50000000 doubles in ptr[ 1 ]
Allocated 50000000 doubles in ptr[ 2 ]
Allocated 50000000 doubles in ptr[ 3 ]
Allocated 50000000 doubles in ptr[ 4 ]
called
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application"s support team for more information.