regarding new operator

K

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.


Thanks in advance
krish
 
A

Alf P. Steinbach

* 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.

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. 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.
 
A

Alf P. Steinbach

* Juha Nieminen:
I thought it returns 0.

No, the ordinary new expression doesn't.

That was pre-standard behavior, which means, before 1998, ten years ago.

In standard C++, i.e. with current compilers, you can write

#include <new>

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

to get the old pre-standard behavior, where on memory depletion new
either hangs, crashes or returns 0.
 
J

jayapal403

* Juha Nieminen:



No, the ordinary new expression doesn't.

That was pre-standard behavior, which means, before 1998, ten years ago.

In standard C++, i.e. with current compilers, you can write

#include <new>

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

to get the old pre-standard behavior, where on memory depletion new
either hangs, crashes or returns 0.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


int* p = new(nothrow) int;

===> here what the nothrow actually indicates.
 
R

Robert Bauck Hamar

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.
 
J

James Kanze

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.)
 
J

James Kanze

* Juha Nieminen:
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.)
 
A

Alf P. Steinbach

* James Kanze:
You mean pre-standard, like having to put the implementation of
your templates in a header file, rather than just declaring them
"export"?

No. "export" is in practice not part of the language, because only one
commercial compiler supports it officially. All the "new" machinery is
supported by modern compilers (possible exception: AIX compiler).

(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.)

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top