473,396 Members | 1,942 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.

regarding new operator


I am a beginner to c++ programming. I just want to know what is the
use of new operator.
If there is no sufficient memory to be allocated then what does the
call to this new return.
Thanks in advance
krish

Jul 12 '07 #1
5 1327
Alf P. Steinbach wrote:
It either hangs, crashes or throws a std::bad_alloc exception.
I thought it returns 0.
Jul 12 '07 #2
On Jul 12, 4:11 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Alf P. Steinbach wrote:
It either hangs, crashes or throws a std::bad_alloc exception.

I thought it returns 0.
No it does not...
http://www.parashift.com/c++-faq-lit....html#faq-16.6

However malloc does..

Abdo Haji-Ali
Programmer
In|Framez

Jul 12 '07 #3
ja********@gmail.com wrote:
On Jul 12, 7:21 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* Juha Nieminen:
In standard C++, i.e. with current compilers, you can write

#include <new>

void foo()
{
int* p = new(nothrow) int;

int* p = new(nothrow) int;

===here what the nothrow actually indicates.
Please learn to quote: Don't quote the signatures, and you should just put
your question under the referenced text.

It should be std::nothrow, and it is a dummy parameter you can send to new,
to make it use an allocation function that does not throw.

--
rbh
Jul 12 '07 #4
On Jul 12, 3:36 pm, "Alf P. Steinbach" <a...@start.nowrote:
* kris:
I am a beginner to c++ programming. I just want to know what is the
use of new operator.
If there is no sufficient memory to be allocated then what does the
call to this new return.
The common new expression doesn't return in the ordinary way
if there is not sufficient memory for the request.
It either hangs, crashes or throws a std::bad_alloc exception.
And on older implementations, it may also return NULL:-). Note
too that hanging is perfectly standards conformant: the standard
doesn't say how long the request will take:-).

And by crashing, I suspect what you mean is that it will return
an apparently good address which will cause the program to crash
when you use it.
The standard mandates the std::bad_alloc exception, but the exact
behavior depends on the operating system and on the standard library
implementation. You don't need to worry about hangs or crashes, because
you can't do anything about them anyway.
Not from inside the C++ program, anyway. In some cases, you may
be able to reconfigure the system so that it will work,
however---I know that this is the case for Linux (which will
cause a crash by default).
Dealing with std::bad_alloc as an exception can be very
difficult because the system is most likely low on memory,
therefore the recommended way (when it matters) is to avoid
dealing with it by installing a so-called "new handler" that
reports failure and terminates the program instead of throwing
an exception.
Totally agreed. There are probably some exceptions, but they're
exceedingly rare. (I think Andy Koenig once wrote an article,
many years back, explaining why you can't reliably handle out of
memory conditions. In the JOPP, if memory serves me correctly,
but it's long enough ago that I'm far from sure.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 12 '07 #5
On Jul 12, 4:21 pm, "Alf P. Steinbach" <a...@start.nowrote:
* Juha Nieminen:
Alf P. Steinbach wrote:
It either hangs, crashes or throws a std::bad_alloc exception.
I thought it returns 0.
No, the ordinary new expression doesn't.
That was pre-standard behavior, which means, before 1998, ten years ago.
You mean pre-standard, like having to put the implementation of
your templates in a header file, rather than just declaring them
"export"?

(In fact, I think that most modern compilers do try to do the
right thing here. And succeed, when the OS doesn't screw things
up.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 12 '07 #6

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

Similar topics

4
by: velthuijsen | last post by:
I was trying out the binary operators and did the following: class mv { private: int* Storage; int StSize; public: mv(int Size = 0);
5
by: Juri | last post by:
Hi all, I have a question regarding the << operator. I wrote a simple queue class, which contains add(char), del() and empty() functions. They works fine. Now I want its output. If I write the...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
12
by: sam_cit | last post by:
Hi Everyone, I often get confused with * and ++ operator when they are used with pointers as to which would be considered first by the compiler, Can anyone explain with some examples to...
6
by: thorsten.schilling | last post by:
Hello everybody, the question is, what is the "golden way" or the best way, if I have a memberfunction in a class, which should return a new instance of an object. For example some class Foo...
3
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a...
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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,...

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.