Subject: Re: [boost] Question for C++ experts about exception allocation failure
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2009-05-18 06:05:07
Emil Dotchevski <emildotchevski_at_[hidden]> writes:
> On Sun, May 17, 2009 at 1:53 AM, Sebastian Redl
> <sebastian.redl_at_[hidden]> wrote:
>> Emil Dotchevski wrote:
>>> How is failure to allocate an exception object handled in various
>>> compilers? 15.1.4 says that "the memory for the temporary copy of the
>>> exception being thrown is allocated in an unspecified way" but I don't
>>> think it specifies behavior for the case when the allocation fails. I
>>> know that some compilers allocate exceptions from the heap, so does
>>> this mean that an attempt to throw any exception could, in theory,
>>> result in a std::bad_alloc being thrown instead? As far as I can tell
>>> such behavior wouldn't violate the C++ standard, but I'm not sure my
>>> interpretation is correct. Anyone?
>> MSVC allocates on the stack.
> Now that you mention this, I remember something else I was wondering
> about MSVC: is the code in the exception handler executed on top of
> the stack, just over the exception object that was put there by the
I don't think exception objects are allocated on the stack in MSVC, and
I don't see how they could be --- if an exception is caught then before
the handler is entered the stack needs to be unwound and all the
destructors run. Having an exception object lurking halfway down the
empty stack space would just confuse things no end.
However, the object in the throw expression (e.g. throw my_class();) is
typically a temporary, and thus stack allocated, and the runtime might
not copy this until a handler has been found.
> Also, you could run out of stack memory. It's likely that in this case
> you'll get abort() just like when you run out of memory for exceptions
> in GCC, unless you have structured exception handling enabled -- and
> in that case would you get some sort of stack overflow exception?
If you run out of stack space in MSVC you do get a stack overflow
structured exception. If you haven't provided a handler then this will
terminate your program.
-- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976