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
6 2281
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
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.
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
Thanks weaknessforcats,
Any comments for my post #4?
Yeah, that's Microsoft. They never outgrew C.
regards,
George
Read your Stroustrup.
The nothrow new: -
int* arr = new (nothrow)[100];
-
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.
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: -
int* arr = new (nothrow)[100];
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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: 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,...
|
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...
| |