skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-new
This page is read only. You can view the source, but not change it.
# <new> **[<new>](https://en.cppreference.com/w/cpp/header/new)** defines `operator new` and `operator delete` for memory allocation. You can overload them for custom allocation behavior or set a custom `new_handler` function to be called if allocation fails. Most code never needs this; just use smart pointers from `<memory>`. This example sets a custom new_handler to be called if memory allocation fails, though modern code uses smart pointers instead. ```cpp // compile: g++ -std=c++11 -o newexample newexample.cpp // run: ./newexample // description: custom new_handler #include <new> #include <iostream> void handle_out_of_memory() { std::cout << "out of memory!\n"; std::exit(1); } int main() { std::set_new_handler(handle_out_of_memory); try { int* p = new int; std::cout << "allocated successfully\n"; delete p; } catch (const std::bad_alloc&) { std::cout << "caught bad_alloc\n"; } return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top