473,396 Members | 1,914 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,396 software developers and data experts.

bad_alloc

Hello everyone,
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;

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).

Both are correct? Please feel free to correct me if I am wrong.
thanks in advance,
George
Dec 26 '07 #1
3 3306
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.
Dec 26 '07 #2
On 2007-12-26 14:37, George2 wrote:
Hello everyone,
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;
Yes.
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).
That is an over generalisation. If, for example, you tried to create a
new instance of a Foo object and they use a specialised pool-allocator
it might throw a bad_alloc when to pool is all used up. But that does
not have to mean that you do not have plenty of memory left for objects
that do not use the pool allocator.

Also, you might have lots of free memory for objects with automatic
storage even though you have used up the free store, which means that
you can create lots of new objects as long as you do not use new.

--
Erik Wikström
Dec 26 '07 #3

"George2" <ge*************@yahoo.comwrote in message
news:68**********************************@e6g2000p rf.googlegroups.com...
Hello everyone,
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;
Not sure what auto_prt has to do with bad_alloc except that it should
take care of cleanup of its internals when an exception occurs in
its construction.
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).

Both are correct? Please feel free to correct me if I am wrong.
Not sure what you're saying here either. If you use an auto_ptr, what
would you do in the catch block? Issue an error to the caller maybe?
You generally use smart pointers to handle the cleanup for you.

One thing to note is that bad_alloc doesn't happen ONLY when you're
out of memory. It can also happen when you need to allocate contiguous
memory and there isn't enough - say for a std::vector. Not exactly the same
thing
as you may have lots of memory but it may be all fragmented.
Dec 26 '07 #4

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

Similar topics

5
by: Asfand Yar Qazi | last post by:
Hi, Just wondering, if I do a: std::set_new_handler(std::terminate) I won't have to worry about a std::bad_alloc being thrown when I do a 'new ...' or a 'new(nothrow) ...', right? Its...
3
by: vulcan.wayne | last post by:
Hi Shouldn't this code cause a core dump when the sys runs out of mem, when there's no default exception handling mechanism. void fn() { SomeObj *so = NULL; so = new SomeObj; // -> no...
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...
3
by: schizoid_man | last post by:
Hi, I have the following code snippets and I get a std::bad_alloc error where I think there should be none. I've attached the relevant bits of the base class, derived class and the .cpp file...
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...
4
by: simbasaurus | last post by:
Hello! I am trying to make sure that I clean up and free all the resources that I allocate in my code. In the following code, I am assuming that if new throws bad_alloc, than the value of...
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...
6
by: George2 | last post by:
Hello everyone, I usually check whether there is bad_alloc thrown to identify whether the allocation is success or not. My question is, Is there a way to disable bad_alloc and just to...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.