473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3313
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
2157
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
2967
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
55882
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
29088
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
18068
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
13057
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
1600
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
492
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
2284
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
7100
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
7126
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
7175
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
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4865
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...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.