473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Catch bad_alloc?

I'm looking for opinions for or against catching std::bad_alloc when I
use the "new" operator. I searched through this newsgroup and the
Internet, and I don't see clear opinions either way. Note that I am
using gc++ version 3.2 on a unix platform, so I'm not concerned with
any points unique to VC++.

Thanks!

Ken

Feb 10 '06 #1
4 5111
kk****@yahoo.com wrote:
I'm looking for opinions for or against catching std::bad_alloc when I
use the "new" operator. I searched through this newsgroup and the
Internet, and I don't see clear opinions either way. Note that I am
using gc++ version 3.2 on a unix platform, so I'm not concerned with
any points unique to VC++.


Depends if you want to handle memory errors. Generally it's a good
idea, but often you can't do very much but close the program. It's
always nice to tell the user that they've run out of memory before
crashing hard :).

It's possible you can dump some other data, to free up memory and
continue. It's your call.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #2
Hi

Ben Pope wrote:
It's possible you can dump some other data, to free up memory and
continue. It's your call.


This is better done in a new_handler, imho.

Markus
Feb 10 '06 #3

"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.de> skrev i
meddelandet news:ne********************@luna.igd.fhg.de...
Hi

Ben Pope wrote:
It's possible you can dump some other data, to free up memory and
continue. It's your call.


This is better done in a new_handler, imho.


That's good at the global level. In a layered application, it might be
better to have a separate catch statement at some of the intermediate
levels.
Bo Persson
Feb 11 '06 #4
On Fri, 10 Feb 2006 19:08:55 +0100, Markus Moll
<mo**@rbg.informatik.tu-darmstadt.de> wrote:
Ben Pope wrote:
It's possible you can dump some other data, to free up memory and
continue. It's your call.


This is better done in a new_handler, imho.


Agreed. OOM (Out of Memory) conditions are usually caused by memory
leaks. You cannot catch or handle memory leaks so it's best to
gracefully shut down the program in the new_handler. See also:

http://www.gotw.ca/publications/mill16.htm
http://www.baus.net/memory-management
Feb 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fao, Sean | last post by:
I'm reading The C++ Programming Language by Bjarne Stroustrup and I was unclear as to the proper way to catch an exception thrown by the new keyword. I was wondering if someobody here could let me...
3
by: 8519428 Wang Tian-da Alex | last post by:
when memory exhaust,how we can catch the new operator exception.
4
by: Noah Roberts | last post by:
The following code does VERY strange things in the debugger. Is there anything wrong with the following code, UB or anything else? #include <iostream> #include <exception> #include <stdexcept>...
5
by: Gary Wessle | last post by:
Hi I am getting this error when running a very similar code like the below, it is made for illustration only. thanks *************************************** **************** error...
14
by: Mohsen | last post by:
Hello everyone, In my program, I have to define many pointers and when I want to compile my program (in UNIX), it gives me the following error: terminate called after throwing an instance of...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
4
by: Alerion | last post by:
Hello everyone, I've been a regular of this forum but this is my first post, generally I can find the answer to my question already, but this time I'm having a somewhat specific problem. For...
3
by: barcaroller | last post by:
I have an object that throws an exception when the constructor fails. I could construct it inside a try/catch block, but then the object is no longer visible outside the block. try { classA ...
5
by: George2 | last post by:
Hello everyone, I am wondering except when there is no memory on heap, are there any other situations when we will get bad_alloc exceptions? For example, invalid input of the size (e.g. very...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.