Topic: Bug in new operator
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/03 Raw View
--Multipart_Thu_Apr__3_17:33:53_1997-1
Content-Type: text/plain; charset=US-ASCII
Hi there!
Thanks for your bug report.
The violation is in your code, not in g++. An operator new should not
return NULL; if it fails to allocate memory, it should throw an
exception. I could not find in the CD2 the specification of the
expected behavior of a new expression when the allocator function
returns NULL. The CD2 actually says an allocator can return NULL, and
it is unspecified whether, in this case, the arguments to the
constructor are evaluated or not. However, it is not stated or
implied that the constructor must not be called in this case, which
makes me believe this is unspecified behavior too.
For the comp.std.c++ people, is this assumption correct? Have I
missed anything?
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
--Multipart_Thu_Apr__3_17:33:53_1997-1
Content-Type: message/rfc822
Date: Tue, 28 Jan 97 09:36:47 PST
From: barto@visionpro.com (David Barto)
Message-Id: <9701281736.AA09083@visionpro.com>
To: bug-g++@prep.ai.mit.edu
Subject: Bug in new operator
If you overload the new operator for a class, and that new then returns
null, then the constructor for the class gets called anyway. Isn't this
in violation of the draft standard?
-barto
// taken from Bruce Eckels book.
#include <iostream.h>
#include <new.h> // size_t definition
void my_new_handler() {
cout << "new handler called" << endl;
}
class nomemory {
nomemory() {
cout << "nomemory::nomemory()" << endl;
}
void* operator new(size_t sz) {
cout << "nomemory::operator new" << endl;
return 0; // "Out of memory"
}
};
main() {
set_new_handler(my_new_handler);
nomemory* nm = new nomemory;
cout << "nm = " << nm << endl;
}
--Multipart_Thu_Apr__3_17:33:53_1997-1--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]