Problems with operator new and constructor execution
Nathan Sidwell
nathan@acm.org
Tue Nov 30 23:39:00 GMT 1999
"Roger Jonsson K (QTX)" wrote:
>
> Hello,
>
> I found a bug with the operator new in gcc (version, platform and makefile
options at the end of the mail).
>
> I wrote my own new operator, it returns NULL when out of memory. In the case
when new returns NULL the constructor of the object is still executed, which of
course results in a core dump (crash) since I do not have any this pointer at
this point (this = NULL).
> void* operator new (size_t s)
> {
> return NULL;
> }
This is wrong. Unless new is declared as not throwing
void *operator new (size_t) throw ()
it must not indicate failure by returning NULL, but by throwing
an exception.
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
Never hand someone a gun unless you are sure where they will point it
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk
More information about the Gcc-bugs
mailing list