Boost logo

Boost :

Subject: Re: [boost] Question for C++ experts about exception allocation failure
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-05-17 01:24:24


On Sat, May 16, 2009 at 7:36 PM, Jeffrey Bosboom <jbosboom_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?
>
> Stroustrup, The C++ Programming Language, Special Edition, page 371:
> "Throwing an exception requires an object to throw.  A C++ implementation
> is required to have enough spare memory to be able to throw bad_alloc in
> case of memory exhaustion.  However, it is possible that throwing some
> other exception will cause memory exhaustion."
>
> That seems to imply that in case of allocation failure when allocating an
> exception, bad_alloc is thrown instead, although he stops short of saying
> precisely that.

The C++ standard is also careful not to specify precisely this
behavior, but at least it's seems clear that throwing bad_alloc in
case there's no memory to throw the requested exception is a valid
(platform-specific) behavior.

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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