[lug] new operator in C++

D. Stimits stimits at idcomm.com
Sat Apr 27 14:02:14 MDT 2002

Stephen Queen wrote:
> In using the new operator in C++ I cannot find whether it is guaranteed
> to initialize the new memory to zero or not. In playing with it, it
> looks like it does, but that might be coincidence. Does anyone know a
> source of information on the net regarding this? Another question
> regarding sources of information on C++. man has section 3 which is a
> good source of information for regular C. Does anyone know of anything
> like that for C++? It sure would come in handy now and then.

Older compilers behaved similar to malloc, and returned NULL if failed.
Newer ones (g++) will throw an exception. To override this and get older
NULL behavior, you can create your own custom allocator, or else do the
easy thing, use the nothrow argument. E.G.:
  myClass* oTheClass = NULL;
  oTheClass = new(nothrow) myClass;

D. Stimits, stimits at idcomm.com

PS: If in doubt, just use (nothrow) and see if the compiler
complains...if it doesn't, then you have a compiler that supports
exception throwing for failures of new.

More information about the LUG mailing list