Re: Writing exception-safe code

Dear Atul,

In general I would agree with you that there is little one could do when the system goes out of memory. I don’t disagree however that the best thing to do is just terminate the execution. IMHO that is not what high-quality software should ever do. It should either try to close gracefully, or try to run with degraded functionality. There have been cases when the software of very expensive hardware (e.g. spacecrafts) just terminates due to an exception, resulting in losses of hundreds of millions. OK, Calitko is not such a critical piece of software but I think we should try our best at writing high-quality software.

Now back to the specific example. If such a small allocation fails, then we are really very much out of memory and there is indeed a high chance nothing more could work, so the safest think could be to exit gracefully. If it were a huge allocation what could fail, then we could catch the exception, abort the operation and continue to work normally. In both cases we have the same considerations regarding exception safety. If we are disciplined, we would always try being exception-safe. Now I agree with you that using scoped allocations or, using exception safe types rather then raw pointers would be much better, but there are issues with doing that in our example. If is not safe to use auto_ptr as element type in STL containers (smart_ptr would do the job though) and assignment from auto_ptr <Derived> to auto_ptr <Base> won’t work either.

Regards,

Peter

Would you like to post a relpy?


This post is a reply to:
Re: Writing exception-safe code
Dear Peter Actually out of memory will anyway terminate the program ( yes I am thinking about the **lazy memory allocation ** scheme use by many platforms ) ---------------------- man malloc --------------------------- By (more...)

No follow-ups yet.