Topic: what happens when new can't allocate ?


Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/11/24
Raw View
d96-mst@nada.kth.se (Mikael Steldal) writes:

> In article <199611190105.RAA07879@taumet.eng.sun.com>,
> clamage@taumet.eng.sun.com (Steve Clamage) wrote:

> >In either kind of failure, is the argument to the constructor evaluated?
> >I believe that is unspecified.
>
> I think that SHOULD be specified.

Why?  Given the expression: "f( i ++ , g() )": if g exits by an
exception, it is unspecified whether i was incremented or not.  Why
should new be any different?

--
James Kanze          +33 3 88 14 49 00           email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: d96-mst@nada.kth.se (Mikael Steldal)
Date: 1996/11/22
Raw View
In article <199611190105.RAA07879@taumet.eng.sun.com>,
clamage@taumet.eng.sun.com (Steve Clamage) wrote:
>You can supply your own additional or replacement versions of operator
>new. Additional versions can return null or throw exceptions on failure.
>(No other failure mode is allowed.)

What about set_new_handler()?

>In either kind of failure, is the argument to the constructor evaluated?
>I believe that is unspecified.

I think that SHOULD be specified.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@taumet.eng.sun.com (Steve Clamage)
Date: 1996/11/22
Raw View
In article puBlyEpfha7X090yn@nada.kth.se, d96-mst@nada.kth.se (Mikael Steldal) writes:
>In article <199611190105.RAA07879@taumet.eng.sun.com>,
>clamage@taumet.eng.sun.com (Steve Clamage) wrote:
>>You can supply your own additional or replacement versions of operator
>>new. Additional versions can return null or throw exceptions on failure.
>>(No other failure mode is allowed.)
>
>What about set_new_handler()?

The new_handler must either provide more storage, exit via an exception,
or call abort() or exit().

Whether or not operator new calls a handler function, in the end
it either returns to its caller or it throws an exception. If it
returns to its caller, it must either return a valid pointer or
a null pointer.

If the new_handler calls abort or exit, the program terminates, and
doesn't return to the caller of operator new. I suppose you could
consider that an additional failure mode of operator new, but it
doesn't strike me as a helpful way to characterize it.

---
Steve Clamage, stephen.clamage@eng.sun.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Ilya P. Ryzhenkov" <ilya@spy.isp.nsc.ru>
Date: 1996/11/17
Raw View
Hello!

I've just subscribed, and saw some messages discussing new behavior
if it can't allocate memory. As it was mentioned both two behavior can
happen :
1. new throws an exception
2. new returns NULL (it may be so when declaring own operator new)
Constructor will not be called for sure, but what about parameters ?
Will they evaluated or not ?
Consider :
 int i=5;
 while (i) { new SomeClass(--i); }
Will it be an endless loop if there will be not enough memory ?
On the other hand, it is common to create lists (for example menu
definition in windowed environment) via
 new MenuItem("File",WM_FILE,
 new MenuItem("Edit",WM_EDIT,
 ...
 0)))...));
But if parameters will *BE* evaluated in not enough memory situation we
are going to loose even more memory! And if each constuctor have a side
effect outside of it's own space - it will be horrible!
Any comments, please ?



--
       Sincerely yours, Ilya
-----------------------------------------------------------------------------
mailto://ilya@spy.isp.nsc.ru
http://spy.isp.nsc.ru
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@taumet.eng.sun.com (Steve Clamage)
Date: 1996/11/19
Raw View
In article 4418@spy.isp.nsc.ru, "Ilya P. Ryzhenkov"
<ilya@spy.isp.nsc.ru> writes:
>Hello!
>
>I've just subscribed, and saw some messages discussing new behavior
>if it can't allocate memory. As it was mentioned both two behavior can
>happen :
>1. new throws an exception
>2. new returns NULL (it may be so when declaring own operator new)
>Constructor will not be called for sure, but what about parameters ?
>Will they evaluated or not ?

We are in a transition period regarding the behavior of "new". I'll
discuss what the draft standard says, although your compiler might
not yet support this behavior.

When you write, for example,
 ... new T(--i) ...
two things happen:
1. The appropriate version of function operator new() is called (the
   global version or a member function of class T).
2. If operator new succeeds, the constructor for class T is called.

Under the rules in the draft standard, this version of the global operator
new must either succeed or throw an exception. It cannot return a null pointer.


An alternative "placement" version of new is also available:
 ... new (nothrow) T(--i) ...
The "nothrow" version of operator new never throws an exception. If
it fails, it returns a null pointer, and the constructor is not called.

You can supply your own additional or replacement versions of operator
new. Additional versions can return null or throw exceptions on failure.
(No other failure mode is allowed.) Replacements for the standard versions
must follow the rules for the standard version they replace.

In either kind of failure, is the argument to the constructor evaluated?
I believe that is unspecified. If you care whether the argument is
evaluated when "new" fails, move the changes you care about outside
the call to "new". In this example, you could write
 --i;
 ... new T(i) ... // or:  ... new (nothrow) T(i) ...
to ensure that "i" is decremented whether or not "new" succeeds.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]