Be prepared for out-of-memory condition

This is from Effective C++ item 7
How to handle out of memory to get information about your class and overload new operator

use nothrow option with new and check null condion

Widget *pw2 = new (nothrow) Widget; // returns 0 if allocation fails if (pw2 == 0) ... // this test may succeed

use set_new_handler and throw std::bad_alloc exception or abort program.

void noMoreMemory() // globel handle defination { cerr << "Unable to satisfy request for memory\n"; abort();                    // #include <cstdlib> } int main() { set_new_handler(noMoreMemory);    // #include <new> might not need int *pBigDataArray = new int[100000000]; ... }

how to set class level handler


class X { public: static new_handler set_new_handler(new_handler p); static void * operator new(size_t size); private: static new_handler currentHandler; }; new_handler X::currentHandler; // sets currentHandler 0 (i.e., null) by default new_handler X::set_new_handler(new_handler p) { new_handler oldHandler = currentHandler; currentHandler = p; return oldHandler; } void * X::operator new(size_t size) { new_handler globalHandler = // install X's std::set_new_handler(currentHandler); // handler void *memory; try { // attempt memory = ::operator new(size); // allocation } catch (std::bad_alloc&) { // restore std::set_new_handler(globalHandler); // handler; throw; // propagate } // exception std::set_new_handler(globalHandler); // restore handler return memory; } void noMoreMemory();//decl. of function to call if memory allocation for X objects fails X::set_new_handler(noMoreMemory);  // set noMoreMemory as X's new-handling function X *px1 = new X; // if memory allocation fails, call noMoreMemory string *ps = new string; // if memory allocation fails, call the global new-handling function  (if there is one) X::set_new_handler(0); // set the X-specific new-handling function to nothing (i.e., null) X *px2 = new X; // if memory allocation fails, throw an exception immediately. (There is no new-handling function for class X.)

create template base class new handler

template<class T> // "mixin-style" base class class NewHandlerSupport { // for class-specific public: // set_new_handler support static new_handler set_new_handler(new_handler p); static void * operator new(size_t size); private: static new_handler currentHandler; }; template<class T> new_handler NewHandlerSupport<T>::set_new_handler(new_handler p) { new_handler oldHandler = currentHandler; currentHandler = p; return oldHandler; } template<class T> void * NewHandlerSupport<T>::operator new(size_t size) { new_handler globalHandler = std::set_new_handler(currentHandler); void *memory; try memory = ::operator new(size); } catch (std::bad_alloc&) { std::set_new_handler(globalHandler); throw; } std::set_new_handler(globalHandler); return memory; } // this sets each currentHandler to 0 template<class T> new_handler NewHandlerSupport<T>::currentHandler; class X: public NewHandlerSupport<X> { ... // as before, but no declarations for }; // set_new_handler or operator new

best article about this

Counting Objects in C++

No comments:

Post a Comment

would you like it. :)