After years of programming, I havent had a situation where reasonable malloc or new would fail (maybe because my mallocs are trully reasonable), though I always check for it.

In my case, apps should gracefully (i hope) close with an appropriate log entry. What would you do in this case? Its interesting to hear your approach - do you wait for resources or close the shop?

有帮助吗?

解决方案

There's pretty much nothing you can do if dynamic allocation fails- there are pretty much no operations written to handle that situation. If it fails, then just let the app crash.

其他提示

I usually have my program shut-down as gracefully as it can, with simple logging of the error message. In C++ I do this by having a catch for std::bad_alloc in main(). By the time the catch executes, destructors called by stack unwinding should have freed some memory, so the logging itself is less likely to fail. I avoid memory allocation (for example by using char * strings rather than std::string strings) in that logging code, to further reduce the chance of the logging failing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top