Topic: new and delete this
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/23 Raw View
stidev@gate.net (Solution Technology) writes:
>If somewhere there is a ... = new SomeClass
>and the SomeClass constructor does a "delete this"
>what is returned as the result of the new?
Undefined behaviour, IMHO.
The `new' expression will return the originally allocated address,
but attempting to access this address - even just to copy a pointer
to it - will result in undefined behaviour.
--
Fergus Henderson | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh | - Linus Torvalds
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/05/15 Raw View
stidev@gate.net (Solution Technology) writes:
>If somewhere there is a ... = new SomeClass
>and the SomeClass constructor does a "delete this"
>what is returned as the result of the new?
>The does standard say anything about it?
I believe the result is undefined. In other words, don't 'delete this'
in a constructor.
Suppose someone derived a class from SomeClass. The compiler would
go ahead and try to run the derived class constructor after SomeClass
had deleted the object -- the compiler doesn't know about the delete.
But even in your simple case, the value of a deleted pointer is
indeterminate. It is not necessarily valid even to inspect the
value of the pointer after calling delete. Once again, the compiler
doesn't know about the delete, and may use the pointer in ways
which lead to a crash.
--
Steve Clamage, stephen.clamage@eng.sun.com
Author: ncm@netcom.com (Nathan Myers)
Date: 1995/05/17 Raw View
>stidev@gate.net (Solution Technology) writes:
>
>>If somewhere there is a ... = new SomeClass
>>and the SomeClass constructor does a "delete this"
>>what is returned as the result of the new?
>
>>The does standard say anything about it?
Perhaps more to the point, the expression "new SomeClass", according
to the standard, will *never* return 0 (what you call NULL).
Any code currently checking for a return value of zero has to
be changed to say (e.g)
if ((p = new(std::nothrow())) != 0) ...
The purpose for this is so that users of new operator
don't have to check both for a 0 result *and* for an exception
too. (That would be a mess!)
Nathan Myers
myersn@roguewave.com
Author: stidev@gate.net (Solution Technology)
Date: 1995/05/14 Raw View
If somewhere there is a ... = new SomeClass
and the SomeClass constructor does a "delete this"
what is returned as the result of the new?
The does standard say anything about it?
If NULL were returned it would sure save having to put a "try" around every
new in the program. Exceptions are OK for faults but ugly to continue
the natural flow when they are expected.
Ken Walter