how to catch out of memory exception in c++
2017-07-18 00:00
756 查看
to properly handle an out-of-memory scenario, you need to set aside some memory so that you can print an error message before exiting. Otherwise, the program will just crash on an unhandled exception while trying to print the error message. To do so, you can allocate a block of memory that is deallocated in the exception handler
// Reserve 16K of memory that can be deleted just in case we run out of memory char* _emergencyMemory = new char[16384]; // ... try { // ... } catch(bad_alloc ex) { // Delete the reserved memory so we can print an error message before exiting delete[] _emergencyMemory; cerr << sizeof(int) * i << " bytes: Out of memory!"; cin.get(); exit(1); } //...
相关文章推荐
- How to fix the dreaded "java.lang.OutOfMemoryError: PermGen space" exception (classloader leaks)
- How to fix the dreaded "java.lang.OutOfMemoryError- PermGen space" exception (classloader leaks)
- How to fix the dreaded "java.lang.OutOfMemoryError: PermGen space" exception (classloader leaks)
- Spark - ERROR Executor: Exception in tjava.lang.OutOfMemoryError: unable to create new native thread
- Out of memory due to hash maps used in map-side aggregation解决办法
- Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
- myeclips报错:!MESSAGE Unhandled event loop exception !STACK 0 java.lang.OutOfMemoryError: unable to cr
- Eclipse运行程序提示:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- MyEclipse启动Tomcat出现Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
- How to discover memory usage of my application in Android
- How to Deal With Out Of Memory Errors
- Exception in thread "http-8080-3" java.lang.OutOfMemoryError: PermGen space
- Exception in thread "Thread-0" java.lang.OutOfMemoryError: GC overhead limit exceeded 解决方法
- Problems with System.OutOfMemoryException At System.String.GetStringForStringBuilder in 32-Bit Managed Solutions
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- intellij IDE Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Exception in thread "http-bio-8080-exec-8" java.lang.OutOfMemoryError: PermGen space
- Jmeter | Uncaught Exception java.lang.OutOfMemoryError: unable to create new native thread.
- 解决办法:matlab Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap
- Hive ERROR: Out of memory due to hash maps used in map-side aggregation