On 17 October 2017 at 16:23, Pedro Alves wrote:
> On 10/17/2017 03:57 PM, David Malcolm wrote:
>
>> Given that we build with -fno-exceptions, what are we guaranteed about
>> what happens when "new" fails? (am I right in thinking that a failed
>> allocation returns NULL in this case?). Is XNEWVEC preferable here?
>
> No, that's incorrect. Even with -fno-exceptions, if new fails,
> then an exception is thrown. And then because the unwinder
> doesn't find a frame that handles the exception, std::terminate
> is called...
>
> You can easily see it with this:
>
> $ cat new-op.cc
> int main ()
> {
> char * p = new char [-1];
> return 0;
> }
>
> $ g++ new-op.cc -o new-op -g3 -O0 -fno-exceptions
> $ ./new-op
> terminate called after throwing an instance of 'std::bad_alloc'
> what(): std::bad_alloc
> Aborted (core dumped)
Right, because operator new is defined inside libstdc++.so (or the
equivalent for a non-GCC stage 1 compiler), which is not typically
built with -fno-exceptions.