Re: bad_alloc
On Jan 5, 1:25 pm, jkherci...@gmx.net wrote:
George2 wrote:
I am wondering except when there is no memory on heap, are
there any other situations when we will get bad_alloc
exceptions? For example, invalid input of the size (e.g.
very huge number or zero or negative number) will cause
exception?
As far as the standard is concerned, new could throw bad_alloc
randomly on Wednesdays: the standard only says that bad_alloc
indicates that the new handler did not succeed in allocating
memory, it does not specify how hard the handler has to try.
From a quality of implementation point of view, however, you
will probably not find such an implementation.
During testing, I use my own custom operator new, precisely
because it has an option which allows the test harness to
request a bad_alloc after n allocations. Unless you have
actually triggers a bad_alloc, you haven't tested any code using
dynamic allocation (and I'd argue that it is morally
irresponsible to release such code). From a quality of
implementation point of view, I'd consider such an option in the
library implementation a big plus.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34