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
|
|
0
|
|
|
|
Reply
|
raghavakrishna.j (11)
|
7/12/2007 1:27:33 PM |
|
* 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: 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?
|
|
0
|
|
|
|
Reply
|
alfps (7389)
|
7/12/2007 1:36:40 PM
|
|
Alf P. Steinbach wrote:
> It either hangs, crashes or throws a std::bad_alloc exception.
I thought it returns 0.
|
|
0
|
|
|
|
Reply
|
nospam270 (2853)
|
7/12/2007 2:11:05 PM
|
|
On Jul 12, 4:11 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> 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-lite/freestore-mgmt.html#faq-16.6
However malloc does..
Abdo Haji-Ali
Programmer
In|Framez
|
|
0
|
|
|
|
Reply
|
abdo.haji.ali (3)
|
7/12/2007 2:17:47 PM
|
|
* 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.
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?
|
|
0
|
|
|
|
Reply
|
alfps (7389)
|
7/12/2007 2:21:31 PM
|
|
On Jul 12, 7:21 pm, "Alf P. Steinbach" <a...@start.no> wrote:
> * 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.
>
> 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.
|
|
0
|
|
|
|
Reply
|
jayapal403 (24)
|
7/12/2007 2:34:14 PM
|
|
jayapal403@gmail.com wrote:
> On Jul 12, 7:21 pm, "Alf P. Steinbach" <a...@start.no> wrote:
>> * 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
|
|
0
|
|
|
|
Reply
|
news7412 (194)
|
7/12/2007 6:09:21 PM
|
|
On Jul 12, 3:36 pm, "Alf P. Steinbach" <a...@start.no> wrote:
> * 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: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
|
|
0
|
|
|
|
Reply
|
james.kanze (9594)
|
7/12/2007 7:32:50 PM
|
|
On Jul 12, 4:21 pm, "Alf P. Steinbach" <a...@start.no> wrote:
> * 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: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
|
|
0
|
|
|
|
Reply
|
james.kanze (9594)
|
7/12/2007 7:36:04 PM
|
|
* James Kanze:
> On Jul 12, 4:21 pm, "Alf P. Steinbach" <a...@start.no> wrote:
>> * 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"?
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.
--
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?
|
|
0
|
|
|
|
Reply
|
alfps (7389)
|
7/12/2007 7:53:30 PM
|
|
|
9 Replies
26 Views
(page loaded in 0.147 seconds)
Similiar Articles: comp.lang.c++.moderated - page 74IEEE format doubles 10 41 (12/1/2003 9:50:18 PM) I have a question regarding the ... operator new[] question 8 56 (12/4/2003 12:50:45 AM) hi, i am having problems ... New Seed7 Release 2006-07-07 - comp.unix.programmer| * With a syntax declaration new operators and statements can be defined. Not new, see e ... Some corrections and clarifications regarding Lisp: On Thu, Jul 13, 2006 at 03:52 ... Single Reader, Single Writer FIFO - comp.programming.threads ...... shared // no copy LockFreeQueue& operator ... Here are some graphs regarding negative effects of sharing ... A0 =A0 LockFreeQueue() > =A0 =A0 : =A0 first(new ... Yet another binary search tree library - comp.lang.cHow about this new example, which should make you happier regarding the '->' operator: struct A { int a; }; struct B { int b; }; struct A *p; struct B b_object ... FAQ Topic - How do I access a frame's content? (2010-04-18) - comp ...Perhaps a new section - "How do I work with iframes ... your point, yes, but also I noticed you used call operator ... Regarding your comment for *that* entry, what do you mean ... Question regarding pragma translate_off/on , synthesis_off/on ...... be the ideal behaviour for a synthesis tool regarding the ... My main point is that we have a new standards ... Things like : -- pragma map_to_operator SUB_UNS_OP ... Adobe, CERT post responses to recent PDF vulnerability report ...... the matter raised last week by ElcomSoft Co. Ltd. regarding ... currentpoint operator 1 82 blacksheep. Extract some pages out of PDF in to a new PDF 2 184 two questions - comp.soft-sys.matlab... tow questions here.The first is I wanted to run a new ... 3 questions regarding code generation - comp.soft-sys.matlab ... ... soft-sys.matlab How can I calculate the following operator ... Const constructor - comp.lang.c++.moderatedAnd in fact there have been > a number of nice debates in this forum regarding ... wrote: > char * p = "abc"; > const char ** paddr = &p ... as I remove the operator new ... what is the dot used for in matlab - comp.soft-sys.matlab ...I am fairly new to matlab and i saw a program on internet. it was regarding finding the centroid of an image. ... multiplication. So we should us the DOT '.' operator. News_StateCodesOperator Training Module Code: Operator Training Module Annex: U.S. Federal Code Links ... New Hampshire. New Jersey. New Mexico. New York. North Carolina. North Dakota. Ohio New Law Regarding SR21/Operator's Crash ReportNew Law Regarding SR21/Operator's Crash Report Effective January 1, 2006, the form titled "Indiana Operator's Vehicle Crash Report"(commonly known as the SR21) will ... 7/2/2012 3:04:13 AM
|