This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++: operator new and disabled exceptions


On 9/27/07, Christophe LYON <christophe.lyon@st.com> wrote:
> Thanks for this simple solution.
>
> Yet, why isn't this the default when using -fno-exceptions ?

I cannot speak for the original decision, but I believe the compiler
should not substitute the nothrow new for the throw new.  My
reasons are as follows.

Compiling without exceptions does not apply to the provided
standard library.  So, the new can still throw.  All the option
does is indicate that the code you are compiling does not care
to handle exceptions, not that exceptions do not exist.

Code written for a throw new does not check return values,
and so is at risk of uncontrolled crashes.  Throwing an exception
into code compiled without exceptions is at least a controlled
crash.

It might be reasonable to emit a warning/error when the throw
new is used with -fno-exceptions.  There are problems with that
approach, though.  The existing builds could fail.  It forces
programmers to explicitly crash rather than implicitly crash,
which tends to make the code not robust when exceptions are
turned back on.  Requiring nothrow new doesn't work well with
header-based libraries that use new in inline functions.

> On 27.09.2007 15:31, Richard Li wrote:
> > Hi,
> >
> > I tried to redefine the global "new" operator like the following code,
> > and it seems to work.
> >
> > #include <new>
> > void * operator new (size_t n)
> > {
> >  return operator new (n, std::nothrow);
> > }
> >
> > void * operator new [] (size_t n)
> > {
> >  return operator new [] (n, std::nothrow);
> > }
> >
> >
> > On 9/27/07, Christophe LYON <christophe.lyon@st.com> wrote:
> >> Hello,
> >>
> >> I am compiling a C++ application, which uses the 'new' operator, with
> >> exceptions disabled (-fno-exceptions).
> >>
> >> Yet, the new operator called is the one that throws an exception in case
> >> of failure. Why is it so ? Isn't it inconsistent with the
> >> -fno-exceptions flag?
> >>
> >> I imagined that compiling with -fno-exceptions would make the compiler
> >> generate calls to new(nothrow) instead.
> >>
> >> In my case, replacing all calls to new to new(nothrow) in the
> >> application is not an option.
> >>
> >> Thanks,
> >>
> >> Christophe.
> >>
> >>
> >
>
>


-- 
Lawrence Crowl


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]