TITLE: using nothrow() in new expressions PROBLEM: sdoherty@nbnet.nb.ca (Sean Doherty) Would someone please tell me whether the new operator returns a null pointer if the memory allocation attempt fails. [...] RESPONSE: jbc@ElSegundoCA.ATTGIS.COM (Jim Chapman) The default operator new function should call (*_new_handler()) if it has been set or return zero otherwise. The handler must either find memory, throw an exception, or terminate the program. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 15 Jul 95 The problem here is that the definition of the function "operator new" has changed, and different compilers, and different releases of the same compiler, may be implementing different definitions. The description above is correct according to the ARM, but not according to the draft C++ standard. (The ARM doesn't mention 'new' throwing an exception, but that's a quibble.) First, let's be clear that when you write, for example, T* p = new T; you have a "new-expression". The expression "new T" results in a call to the appropriate version of the function "operator new", followed by invocation of the appropriate constructor, if any, for type T. The result of the new-expression is a pointer to the T object. According to the draft C++ standard, the default operator new() attempts to allocate memory. If that fails, it calls an installed new-handler if there is one, and retrys. If an allocation fails and there is no new-handler, "operator new" throws the "bad_alloc" exception, it never returns zero. Another version of "operator new", used with placement syntax, operates in the same way, except that it never throws an exception, but returns a null pointer on failure. You select this function with the syntax, for example, #include // or perhaps ... T* p = new (nothrow()) T; "nothrow" is an empty class defined in the "new" header, and serves only to select the alternative version of "operator new". Your compiler may not yet support this latest addition to C++. Once all compilers support the change, you can write portable code which relies on either getting a null return or an exception on allocation failure. At the moment, there is variation among compilers.