473,405 Members | 2,338 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,405 software developers and data experts.

Doubt on new operator

Do we need to check for error condition when we use new operator in C++
instead of malloc?

Thanks.

Jul 17 '06 #1
6 1565

edu.mvk wrote:
Do we need to check for error condition when we use new operator in C++
instead of malloc?
By default, operator new will throw std::bad_alloc exception if memory
allocation fails. However, if you don't like exception handling and
want old C-style null pointer check, you can use operator
new(std::nothrow).

In this way, null pointer is returned to indicate memory failure. You
have to include <newto use this feature.

Jul 17 '06 #2
edu.mvk wrote:
Do we need to check for error condition when we use new operator in C++
instead of malloc?
Sure! Well, on second thought it may depend on the project requirements.
However, remember that new() will not return 0 but throw bad_alloc if it
fails.
Best

Kai-Uwe Bux
Jul 17 '06 #3
edu.mvk wrote:
Do we need to check for error condition when we use new operator in C++
instead of malloc?

Thanks.
See also the other replies. A more general point however is to ask what
we can really do about it if memory is actually exhausted? Not much I
think other than stop gracefully in the new handler. We certainly can't
acquire more if there's none left to acquire, unless we create some kind
of "overflow" mechanism such as virtual memory. Most OS these days
supply such a mechanism, unless we're talking about an embedded control
environment where you would be using a special-purpose allocator anyway.

Cheers
Jim.
Jul 17 '06 #4
In article <rQ*****************@newsfe1-win.ntli.net>,
James Bannon <ja**********@ntlworld.comwrote:
edu.mvk wrote:
Do we need to check for error condition when we use new operator in C++
instead of malloc?

Thanks.

See also the other replies. A more general point however is to ask what
we can really do about it if memory is actually exhausted? Not much I
think other than stop gracefully in the new handler.
Try to be more imaginative. Let's say we are working in a system and at
least two of three different modules have to be in place for the program
to work correctly, the third module is a "nice to have" feature. If
there isn't enough RAM for this third feature, we want the user to still
have the option of continuing without it...
Jul 17 '06 #5
Daniel T. wrote:
Do we need to check for error condition when we use new operator in C++
instead of malloc?

Thanks.

See also the other replies. A more general point however is to ask what
we can really do about it if memory is actually exhausted? Not much I
think other than stop gracefully in the new handler.

Try to be more imaginative. Let's say we are working in a system and at
least two of three different modules have to be in place for the program
to work correctly, the third module is a "nice to have" feature. If
there isn't enough RAM for this third feature, we want the user to still
have the option of continuing without it...
Who is supposed to use new? The module, or the program that wants to use the
feature? In the first case, the program can catch the exception and do
whatever it wants, the module just has to ensure his initialization is
exception safe, no need to catch the exception himself.

--
Salu2
Jul 17 '06 #6
Daniel T. wrote:
In article <rQ*****************@newsfe1-win.ntli.net>,
James Bannon <ja**********@ntlworld.comwrote:
edu.mvk wrote:
Do we need to check for error condition when we use new operator in C++
instead of malloc?
See also the other replies. A more general point however is to ask what
we can really do about it if memory is actually exhausted? Not much I
think other than stop gracefully in the new handler.

Try to be more imaginative. Let's say we are working in a system and at
least two of three different modules have to be in place for the program
to work correctly, the third module is a "nice to have" feature. If
there isn't enough RAM for this third feature, we want the user to still
have the option of continuing without it...
Or, we might have memory pools (cf.
http://www.parashift.com/c++-faq-lit...tml#faq-11.14), and one
pool is exhausted so we could switch to another.

Cheers! --M

Jul 17 '06 #7

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

Similar topics

6
by: praba kar | last post by:
Dear All, I have doubt regarding sorting. I have a list that list have another list (eg) list = ,,] I want to sort only numeric value having array field. How I need to do for that.
1
by: Srini | last post by:
I was reading the "Exceptional C++" of Herb Sutter. In an example, he mentions the following. // In some library header: namespace N { class C{}; } int operator+(int i, N::C) { return i+1; }...
4
by: Amit Bhatia | last post by:
Hi, I am trying to define the assignment operator for a class called Cell. I am bit confused between the following two statements: const Cell& Cell::operator=(const Cell &r) { \\ all the...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
4
by: pradeep kaltari | last post by:
Hello friends, I have a doubt in the working of IN operator. I have a schema by name 'test_schema'. If you look at the following query the expected output is "1", but nothing is displayed. SELECT...
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
8
by: naveen.dixit | last post by:
I have a piece of code #include<iostream> using namespace std; main() { int x=12; int *p = &x; cout<<endl<<p<<endl<<*p++<<endl; cout<<endl<<p<<endl<<*p<<endl;
11
by: subramanian100in | last post by:
In http://www.parashift.com/c++-faq-lite/friends.html#faq-14.5 the following is mentioned: (member functions don't allow promotion of the left hand argument, since that would change the...
8
by: Tarique | last post by:
Hello. #include<stdio.h> int main(void) { int i=- -2; printf("%d",i); return 0; }
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
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
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
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
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.