George2 <ge*************@yahoo.comwrote:
Please help to comment whether my following understanding is correct,
1. whether or not we are using auto_ptr to allocate new object on heap
(using new), there may be bad_alloc exceptions;
Right. Any time *any* code uses 'new' a bad_alloc exception may result.
This includes vector::reserve() and vector::push_back() for example.
2. when we met with such exceptions, we catch it (bad_alloc) and try
to mininize the operation in catch handler block (since when bad_alloc
occurs, it means memory is running out, we can not do anything complex
in handler).
When a bad_alloc occurs, it means the allocation in question has failed.
It all depends on how big that allocation attempt was as to whether
"memory is running out" or not.
Catching a bad_alloc is only useful if you have a situation where there
are two ways of doing something, one is fast but requires lots of
memory, while the other way is slow and requires little memory. You can
attempt to grab the memory, if the attempt succeeds you can do it the
fast way. If the attempt fails, you will have to do the job using the
slow method.