"Jacob" <ja***@yahoo.com> wrote in message
news:2Z********************@telenor.com...
It is common practice (I've heard) to always catch
allocation exceptions from "new". How is done in
practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a
multi-million LOC, GUI based application).
In practice, you first of all make sure that your code
is exception-safe. That is, if an exception is thrown,
the current operation is stopped or cancelled gracefully.
In particular, you should use RAII to avoid memory
or resource leaks.
Then you should only need to care about error handling at
the top-level of a program (e.g. the agent that generates
commands, or top-level functions that handle user-
generated commands). Only there do you care and know how
to report the failure of an operation.
Sometimes, at an intermediate level, it is possible
to release resources or reset/alter some environment
parameters to attempt the same operation again.
It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?
What is 'absolutely' required?
Objects should be allocated dynamically when it is
suitable to manually control their lifetime, and stack-
based or global objects are not an adequate solution.
This is a matter of design.
Also, dynamic allocation does not imply using 'new'
(think in terms of containers when possible...).
When a stack can overflow and how this error condition
is handled is unfortunately a platform-specific detail.
There is no fully portable solution in standard C++.
Sometimes large objects or data stacks are indeed
allocated on the heap just because this provides better
control over object allocations and error handling
(e.g. a recursive algorithm can be rewritten to use
an std::vector rather than the program stack, to better
detect and handle excessive recursion).
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com