This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Wanted: if new returns NULL, don't run constructor
- To: egcs at egcs dot cygnus dot com, gkm at eng dot ascend dot com
- Subject: Re: Wanted: if new returns NULL, don't run constructor
- From: mrs at wrs dot com (Mike Stump)
- Date: Fri, 26 Feb 1999 14:43:25 -0800
- Cc: balsup at eng dot ascend dot com
> Date: Fri, 26 Feb 1999 13:05:43 -0700
> From: Greg McGary <gkm@eng.ascend.com>
> To: egcs@egcs.cygnus.com
> Cc: balsup@eng.ascend.com, gkm@eng.ascend.com
> In a legacy embedded system that's tight on memory, I can't afford
> exception handling.
-fno-exceptions -fno-rtti will get you what you want.
> Neither is it useful to handle memory allocation failures with a
> new_handler. What I'd like to see is `new OBJ' return NULL on
> failure,
Please buy a new book on C++. In C++ you can say new (nothrow) OBJ
and have it do what you want.
For legacy code you can
#define new new (nothrow)
and get mostly what you want.
> and for OBJ's constructor to detect (this==NULL) and return
> immediately.
If all the above fails, you can use -fcheck-new. RTFM:
@item -fcheck-new
Check that the pointer returned by @code{operator new} is non-null
before attempting to modify the storage allocated. The current Working
Paper requires that @code{operator new} never return a null pointer, so
this check is normally unnecessary.
An alternative to using this option is to specify that your
@code{operator new} does not throw any exceptions; if you declare it
@samp{throw()}, g++ will check the return value. See also @samp{new
(nothrow)}.