Boost logo

Boost :

Subject: Re: [boost] Question for C++ experts about exception allocation failure
From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2009-05-17 04:53:36


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?
>
GCC allocates exceptions on the heap. If that fails, it will allocate
them in the emergency storage instead. The emergency storage is a small
buffer (64 slots of a few kB each - used to be 16 slots, but I changed
that when I implemented exception propagation) allocated statically.
If allocation in emergency storage fails, GCC aborts the process.

The way the common C++ ABI is specified, I think every compiler that
follows it will have to do a very similar thing. There's a
__cxa_allocate_exception function specified. So Sun, IBM and HP's
compilers probably do the same thing. But I can't vouch for it.
http://www.codesourcery.com/public/cxx-abi/abi-eh.html#cxx-throw

MSVC allocates on the stack.

Sebastian


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk