Article ID: 167733 - Last Review: January 9, 2006 - Revision: 5.2 Operator new does not throw a bad_alloc exception on failure in Visual C++This article was previously published under Q167733 SYMPTOMS Operator new does not throw a bad_alloc exception when it
fails. It simply returns a null pointer. RESOLUTION Operator new does call the new handler function after it
fails to procure the requested block of memory, but before it returns the null
pointer. An application could install a new handler to throw a bad_alloc
exception as follows: To install the new handler before your global objects are initialized, create a class that sets the new handler in its constructor and installs the old new handler in its destructor. Create a global object of that type and use the init_seg pragma to force this global object to be initialized before any of your global objects. The example below demonstrates this. It also demonstrates the use of _set_new_mode to cause a failed malloc call to generate an exception. Note that to do this, the code below must reside in its own source file. You cannot change the initializations segment more than once per translation unit (source file) with the pragma init_seg. STATUS This is a bug in Microsoft's implementation of operator new
as this in not in conformance with the ANSI C++ Standard. REFERENCES For additional information, please see the following
articles in the Visual C++ 5.0 online Help:
Using _set_new_handler http://msdn.microsoft.com/en-us/library/aa278270(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa278270(VS.60).aspx) _set_new_handler http://msdn.microsoft.com/en-us/library/aa298584(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa298584(VS.60).aspx) _set_new_mode http://msdn.microsoft.com/en-us/library/aa298585(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa298585(VS.60).aspx) init_seg http://msdn.microsoft.com/en-us/library/aa273904(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa273904(VS.60).aspx) APPLIES TO
| Article Translations
|
Back to the top
