This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Using gnu::unique_ptr to avoid manual cleanups (was Re: [PATCH 2/2] use unique_ptr some)
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Pedro Alves <palves at redhat dot com>
- Cc: David Malcolm <dmalcolm at redhat dot com>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, tbsaunde+gcc at tbsaunde dot org
- Date: Tue, 17 Oct 2017 16:25:44 +0100
- Subject: Re: Using gnu::unique_ptr to avoid manual cleanups (was Re: [PATCH 2/2] use unique_ptr some)
- Authentication-results: sourceware.org; auth=none
- References: <20170731234607.21952-1-tbsaunde+gcc@tbsaunde.org> <20170731234607.21952-3-tbsaunde+gcc@tbsaunde.org> <1508252229.29092.96.camel@redhat.com> <b6cc23fc-6edc-fa08-77ae-e740b42f913c@redhat.com>
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.