Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::set_new_handler

      From cppreference.com
      <cpp‎ |memory‎ |new
       
       
      Utilities library
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
       
      Defined in header<new>
      std::new_handler set_new_handler(std::new_handler new_p)throw();
      (until C++11)
      std::new_handler set_new_handler(std::new_handler new_p)noexcept;
      (since C++11)

      Makesnew_p the new global new-handler function and returns the previously installed new-handler.

      Thenew-handler function is the function called byallocation functions whenever a memory allocation attempt fails. Its intended purpose is one of three things:

      1) make more memory available,
      2) terminate the program (e.g. by callingstd::terminate),
      3) throw exception of typestd::bad_alloc or derived fromstd::bad_alloc.

      The default implementation throwsstd::bad_alloc. The user can install their ownnew-handler, which may offer behavior different than the default one.

      Ifnew-handler returns, the allocation function repeats the previously-failed allocation attempt and calls thenew-handler again if the allocation fails again. To end the loop,new-handler may callstd::set_new_handler(nullptr): if, after a failed allocation attempt, allocation function finds thatstd::get_new_handler returns a null pointer value, it will throwstd::bad_alloc.

      At program startup,new-handler is a null pointer.

      This function is thread-safe. Every call tostd::set_new_handlersynchronizes-with (seestd::memory_order) the subsequentstd::set_new_handler andstd::get_new_handler calls.

      (since C++11)

      Contents

      [edit]Parameters

      new_p - pointer to function of typestd::new_handler, or null pointer

      [edit]Return value

      The previously-installed new handler, or a null pointer value if none was installed.

      [edit]Example

      Run this code
      #include <iostream>#include <new> void handler(){std::cout<<"Memory allocation failed, terminating\n";    std::set_new_handler(nullptr);} int main(){    std::set_new_handler(handler);try{while(true){            newint[1000'000'000ul]();}}catch(conststd::bad_alloc& e){std::cout<< e.what()<<'\n';}}

      Possible output:

      Memory allocation failed, terminatingstd::bad_alloc

      [edit]See also

      allocation functions
      (function)[edit]
      obtains the current new handler
      (function)[edit]
      function pointer type of the new handler
      (typedef)[edit]
      exception thrown when memory allocation fails
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/new/set_new_handler&oldid=153365"

      [8]ページ先頭

      ©2009-2025 Movatter.jp