Re: bad alloc
On Aug 30, 2:54 pm, Goran <goran.pu...@gmail.com> wrote:
On Aug 30, 3:26 pm, Paul <pchris...@yahoo.co.uk> wrote:
[...]
I think that pre-STL it was pretty much standard practise to check for
memory allocation failures for example:
float* m1 = new float[16];
if(!m1){
//output an exit msg.
exit();}
No, that's completely wrong. new float[whatever] should throw just as
vector.push_back should throw. This code is just completely wrong.
Either you'd get to the "if" with m1 pointing to allocated memory and
not being NULL, either exception is thrown and you can't possibly
assign to m1 not reach the "if".
It depends on what he means by "pre-STL". When I was learning
C++, operator new didn't raise an exception, for the simple
reason that exceptions weren't yet part of the language. And
while the usual practice was to replace the new_handler to
ensure a clean shutdown, if this wasn't done, you did check the
results of every new.
That was a long time ago, however.
[...]
When I learned C++ I was taught that the return from new should always
be checked for null.
You were taught wrong. What compiler was that? Microsoft's, version 5
or 6, no MFC? Those had had nonstandard behavior (operator new was
returning NULL).
Not "nonstandard", but "pre-standard".
--
James Kanze