Topic: Operator new: memory allocation error
Author: stanley@West.sun.com (Stanley Friesen [Contractor])
Date: 1998/12/15 Raw View
In article <756555$g7i$1@nnrp1.dejanews.com>, <AllanW@my-dejanews.com> wrote:
>
>
>> Section 3.7.3.1, p3:
>> "An allocation function that fails to allocate storage can invoke the
> ^^^
>> currently installed new_handler (18.4.2.2), if any [Note: A
>
>Should this be "shall invoke"? Or is this really optional?
>
>Is a conforming compiler allowed to ignore the currently-installed
>new_handler function?
>
I do not have my copy of the standard available here, but I *believe* this
clause is part of the description of what is allowed in *user* *defined*
allocation functions, for which it reall is optional. I am fairly certain
that the description of the *default* allocation function(s) clearly
require such a call.
[I will try to check sometime soon].
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Pete Becker <petebecker@acm.org>
Date: 1998/12/16 Raw View
AllanW@my-dejanews.com wrote:
>
> > Section 3.7.3.1, p3:
> > "An allocation function that fails to allocate storage can invoke the
> ^^^
> > currently installed new_handler (18.4.2.2), if any [Note: A
>
> Should this be "shall invoke"? Or is this really optional?
>
> Is a conforming compiler allowed to ignore the currently-installed
> new_handler function?
Take a look at the last paragraph preceding this clause. This clause
describes what an allocation function that is defined in the program is
required to do. It is not required to invoke the current new_handler,
but it is permitted to do so.
--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/12/16 Raw View
Stanley Friesen [Contractor] wrote:
>
> In article <756555$g7i$1@nnrp1.dejanews.com>, <AllanW@my-dejanews.com> wrote:
> >
> >
> >> Section 3.7.3.1, p3:
> >> "An allocation function that fails to allocate storage can invoke the
> > ^^^
> >> currently installed new_handler (18.4.2.2), if any [Note: A
> >
> >Should this be "shall invoke"? Or is this really optional?
> >
> >Is a conforming compiler allowed to ignore the currently-installed
> >new_handler function?
> >
> I do not have my copy of the standard available here, but I *believe* this
> clause is part of the description of what is allowed in *user* *defined*
> allocation functions, for which it reall is optional. I am fairly certain
> that the description of the *default* allocation function(s) clearly
> require such a call.
Correct. Section 18.4.1 defines the required behavior of the default
allocation functions, and it includes a call to new_handler() for the
non-placement forms.
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/12/15 Raw View
> Section 3.7.3.1, p3:
> "An allocation function that fails to allocate storage can invoke the
^^^
> currently installed new_handler (18.4.2.2), if any [Note: A
Should this be "shall invoke"? Or is this really optional?
Is a conforming compiler allowed to ignore the currently-installed
new_handler function?
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: MarceloChaves@hotmail.com
Date: 1998/12/13 Raw View
Gentlepeople,
I noticed that diferent C++ compilers implement different actions when a
memory allocation via new fails. For example, the g++ (gnu) compiler (Linux)
aborts in case the (internal) malloc fails (it printfs and aborts); some
compilers raise an exception (I guess an old Borland compiler used something
called "xlalloc"), according Bjarne's C++ reference book, the new operator
should return NULL ( (void *) 0), and so on.
Does anybody know:
a) Which should be the standard behaviour in case new fails? b) How the best
way to write portable code using new (override new operator? use of malloc +
placement new? go back to malloc??) c) Am I missing something here? :-)
Thanks!
Marcelo Chaves.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/12/14 Raw View
MarceloChaves@hotmail.com wrote:
>
> Gentlepeople,
>
> I noticed that diferent C++ compilers implement different actions when a
> memory allocation via new fails. For example, the g++ (gnu) compiler (Linux)
> aborts in case the (internal) malloc fails (it printfs and aborts); some
> compilers raise an exception (I guess an old Borland compiler used something
> called "xlalloc"), according Bjarne's C++ reference book, the new operator
> should return NULL ( (void *) 0), and so on.
>
> Does anybody know:
>
> a) Which should be the standard behaviour in case new fails? b) How the best
> way to write portable code using new (override new operator? use of malloc +
> placement new? go back to malloc??) c) Am I missing something here? :-)
a): Section 3.7.3.1, p3:
"An allocation function that fails to allocate storage can invoke the
currently installed new_handler (18.4.2.2), if any [Note: A
program-supplied allocation function can obtain the address of the
currently installed new_handler using the set_new_handler function
(18.4.2.3).] If an allocation function declared with an empty
exception-specification (15.4), throw(), fails to allocate storage, it
shall return a null pointer. Any other allocation function that fails to
allocate storage shall only indicate failure by throwing an exception of
class std::bad_alloc(18.4.2.1) or a class derived from std::bad_alloc."
Seciton 5.3.4p13 adds:
"If the allocation function returns null, initialization shall not be
done, the deallocation function shall not be called, and the value of
the new-expression shall be null."
You can use the non-throwing version by using 'new(nothrow)'.
b) There's no way you can guarantee portability to arbitrary
implementations that haven't yet implemented the standard. The best you
can do is see what works on the implementations you actually need to
port to. Sorry.
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]