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

bad_alloc in new

200 100+
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 check the returned pointer NULL or not to identify allocation success or not -- which from function point of view, is as correct as the way to catch bad_alloc? Windows platform/Visual Studio is ok. I always see code does not check bad_alloc and just check the return pointer.

(My solution is to select Enable C++ Exception to No in Code Generation option in Visual Studio, not sure whether it is the most correct way.)


thanks in advance,
George
Jan 27 '08 #1
6 2279
Savage
1,764 Expert 1GB
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 check the returned pointer NULL or not to identify allocation success or not -- which from function point of view, is as correct as the way to catch bad_alloc? Windows platform/Visual Studio is ok. I always see code does not check bad_alloc and just check the return pointer.

(My solution is to select Enable C++ Exception to No in Code Generation option in Visual Studio, not sure whether it is the most correct way.)


thanks in advance,
George
Yes,goggle for no throw new
Jan 27 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Windows platform/Visual Studio is ok. I always see code does not check bad_alloc and just check the return pointer.
Yeah, that's Microsoft. They never outgrew C.
Jan 27 '08 #3
George2
200 100+
Thanks Savage,


1.

You mean using std::nothrow?

2.

No need to link with nothrownew.obj in order to use std::nothrow?

Yes,goggle for no throw new

regards,
George
Jan 28 '08 #4
George2
200 100+
Thanks weaknessforcats,


Any comments for my post #4?

Yeah, that's Microsoft. They never outgrew C.

regards,
George
Jan 28 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
Read your Stroustrup.

The nothrow new:
Expand|Select|Wrap|Line Numbers
  1. int* arr = new (nothrow)[100];
  2.  
will return 0 when there is no heap.

Bjarne says this is to allow applications with pre-exception coding (read relic code) to compile and execute by checking for NULL on the return from new instead of having to change the application to accommodate bad_alloc.

That means you do not write new code using nothrow.
Jan 28 '08 #6
George2
200 100+
Thanks weaknessforcats!


My question is anwered. And from your reply, I think there is no need to link with nothrownew.obj.

Read your Stroustrup.

The nothrow new:
Expand|Select|Wrap|Line Numbers
  1. int* arr = new (nothrow)[100];
  2.  
will return 0 when there is no heap.

Bjarne says this is to allow applications with pre-exception coding (read relic code) to compile and execute by checking for NULL on the return from new instead of having to change the application to accommodate bad_alloc.

That means you do not write new code using nothrow.

regards,
George
Jan 29 '08 #7

Sign in to post your reply or Sign up for a free account.

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...
3
by: George2 | last post by:
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...
0
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: 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: 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
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...
0
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...

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.