🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1641

Re: Operator new Question

#1641
From: Steve Clamage
Date: Mon, 07 Aug 1995 16:23
58 lines
3274 bytes
In article 2i8@gabi.gabi-soft.fr, kanze@gabi-soft.fr (J. Kanze) writes:
>Steve Clamage (clamage@Eng.Sun.COM) wrote:
>
>|> The results of using "delete" on a pointer value obtained from a placement-
>|> new are undefined. Any result is allowed. It is invalid C++, but no
>|> diagnostic is required.
>
>Can this be true?  I always thought that the results of using delete
>were to call the destructors, and then call operator delete().  If the
>memory was not obtained by calling operator new( size_t ), then the
>results are undefined, but in the example given, the memory was obtained
>by the placement new calling operator new( size_t ).
>
>Does this mean that I cannot delete memory obtained by:
>
>	p = new ( nothrow() ) T ;
>
>This would sort of make this version of new useless.

My response was sloppy and superficial. Sorry. Here is the text from the
draft standard:

------------------------------------
5.3.5 Delete [expr.delete] 
The delete­expression operator destroys a complete object (1.6) or array created by
a new­expression.
	delete­expression:
		:: opt delete cast­expression
		:: opt delete [ ] cast­expression
The first alternative is for non­array objects, and the second is for arrays.  The
operand shall have a pointer type.  The result has type void. In either alternative,
if the value of the operand of delete is the null pointer the operation has no effect.
Otherwise, in the first alternative (delete object), the value of the operand of
delete shall be a pointer to a non­array object created by a new­expression without
a new­placement specification, or a pointer to a sub-object (1.6) representing a
base class of such an object (10), or an expression of class type with a conversion
function to pointer type (_class.conv,fct_) which yields a pointer to such an object.
If not, the behavior is undefined.  In the second alternative (delete array), the
value of the operand of delete shall be a pointer to an array created by a
new­expression without a new­placement specification.  If not, the behavior is
undefined.
------------------------------------

In the cases in question, the returned pointer refers to an object that was
created by a non-placement "operator new". It was passed along by a
placement version of "operator new", but without alteration. Thus, it must
be safe to delete the pointer (modulo double delete or other pointer errros
of course).

Regarding the "nothrow" version of "operator new", the language in the draft
says the effects are the "same" as for the non-placement version, and gives
semantics that are the same except for behavior on allocation failure.

I think we need to add language about using delete to section
18.4.1  Storage allocation and deallocation [lib.new.delete] 
to make it plain that James's example above is valid. 
---
Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID: <405elr$4br@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet
References: <4051al$2i8@gabi.gabi-soft.fr>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Operator new Question
Date: 7 Aug 1995 16:23:23 GMT
Organization: Sun Microsystems Inc.
Message-ID: <405elr$4br@engnews2.Eng.Sun.COM>
References: <4051al$2i8@gabi.gabi-soft.fr>
Reply-To: clamage@Eng.Sun.COM
NNTP-Posting-Host: taumet.eng.sun.com