77

I was wondering, will new T still throw bad_alloc if I compile my program using the -fno-exceptions option to disable exception handling?

Or will the compiler (GCC and clang support that option) implicitly transform the use of new T to new (nothrow) T?

5
  • 1
    Interesting question. But, why do you want to disable it?
    – user180326
    May 18, 2011 at 18:54
  • 13
    I don't want to disable it :) May 18, 2011 at 19:04
  • Just curious, are you dealing with code that routinely throws bad_alloc? May 18, 2011 at 19:13
  • 34
    I'm not dealing with any code. I'm only dealing with thoughts in the air. May 18, 2011 at 19:16
  • 1
    Since the C++ Standard does not consider this case, and since many Standard Library implementations assume (because of the Standard) that allocators always return a valid block of memory (it's guaranteed), you would have to forgo the Standard Library in such a program... and thus provide your own new and delete, I surmise. May 19, 2011 at 7:13

4 Answers 4

73

The way I understand it, operator new is defined by libstdc++. If you now compile your own code with -fno-exceptions, you cannot catch any exceptions, but you will still be linking against the normal version of libstdc++, which does throw an exception.

So yes, new T will throw an exception, even with -fno-exception.

However, if you compiled libstdc++ with -fno-exception as well, things become different. Now, new T cannot throw an exception but, if I read the libstdc++ manual right it will call abort() instead.

It seems that, if you want your new T to return NULL on failure, the only way is to explicitely specify nothrow...

1
  • 1
    @nos This is a good contrast to the accepted answer, and really shows, in more detail, what is going on with the case of cat bad_alloc.cpp, above.
    – 9301293
    Sep 19, 2018 at 17:55
40

I can't give a definitive answer to all the perks around -fno-exceptions, just the observations on a 32 bit linux machine, gcc 4.5.1 - bad_alloc is thrown with and without -fno-exceptions

[21:38:35 1 ~/tmp] $ cat bad_alloc.cpp

int main()
{
    char* c = new char[4000000000U];
}
[21:38:58 1 ~/tmp] $ g++ bad_alloc.cpp
[21:39:06 1 ~/tmp] $ ./a.out
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
[21:39:07 1 ~/tmp] $ g++ -fno-exceptions bad_alloc.cpp
[21:39:16 1 ~/tmp] $ ./a.out
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
2
  • 31
    wouldn't you need to rebuild the C++ Standard library with -fno-exceptions in order to get the fully desired result?
    – rubenvb
    May 18, 2011 at 20:46
  • Clang stops code with throw from compiling when exceptions are disabled, does GCC have similar behaviour? Dec 27, 2016 at 17:41
10

It's not a definitive answer, but the GCC Manual (see the section "Doing Without") has this:

Before detailing the library support for -fno-exceptions, first a passing note on the things lost when this flag is used: it will break exceptions trying to pass through code compiled with -fno-exceptions whether or not that code has any try or catch constructs. If you might have some code that throws, you shouldn't use -fno-exceptions.

The way I read that, you might have to explicitly ask for the nothrow version of new to be completely safe.

1
0

In many exception-handling systems, if routine "foo" calls "bar", which in turn calls "moo", and "moo" throws an exception, the only way that exception can cleanly make it back to "foo" is if "bar" has code to handle the exception. Even if "bar" is going to let the exception propagate uncaught, it will generally have to ensure that its local variables get properly destroyed before execution is allowed to leave scope. This will require adding extra code to "bar"; in most systems, some of that code will have to execute even if no exception is thrown.

BTW, on some ARM processors like the Cortex M3, or like the Arm7 running in ARM mode, if the caller is also going to be running in ARM mode, one could allow for exceptions without any execution-time cost by having a "normal" subroutine return go to LR+4 (four bytes beyond the normal return address) and have an exceptional exit go to LR (which would then be a 4-byte branch instruction). Such behavior would be contrary to normal practice on the ARM, though, and such a design would not port nicely to the Cortex M0.

1
  • 2
    So, given that information, does GCC convert new to new (nothrow)? ;-p May 18, 2011 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.