Topic: Legal operands to delete


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Wed, 3 Nov 1993 22:43:13 GMT
Raw View
In article <2b7673$2nm@agate.berkeley.edu> hilfinger@CS.Berkeley.EDU writes:
>The ARM says "The operand of delete must be a pointer returned by
>new."  Presumably,  the result of converting a pointer returned by new
>does not, in general, constitute a pointer returned by new...

Gosh!  I hope that is not true for the general case.  I sure hope that
given a pointer to some type T, I can convert that to a void* and back
again to a T* and end up with the same value!

>What precisely are the operations that "dirty" a pointer so that it is
>no longer a "pointer returned by new"?  I am not interested in
>implementation-based definitions; I can easily understand the
>circumstances under which obvious implementations of delete will
>"work."  I am interested in the official answer of the Standard.

I do not think this question has been answered yet (either within X3J16
or elsewhere).

--

-- Ronald F. Guilmette, Sunnyvale, California -------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------




Author: hilfingr@tully.CS.Berkeley.EDU (Paul N. Hilfinger)
Date: 3 Nov 1993 02:49:07 GMT
Raw View
The ARM says "The operand of delete must be a pointer returned by
new."  Presumably,  the result of converting a pointer returned by new
does not, in general, constitute a pointer returned by new.  For
example (and for motivation),

 struct A {
     some stuff;
 };

 struct B {
     more stuff;
 };

 struct C : public A, public B {
     even more stuff;
 };

 static B* Q = new C; // Implicit conversion to accessible
    // base class B*.

 main()
 {
     delete Q;   // Illegal, I presume (for good
    // reason) but not generally caught,
    // I suspect.
 }

This raises a small, really nit-picky technical question.  Consider now

 void mungeIt(C* foo)
 {
     delete foo;
 }

 void blastIt(C*& bar)
 {
     mungeIt(bar);
 }

        ... blastIt(aPointerToC); ...

Passing bar (of type C*&) to mungeIt (type C*) involves another
implicit conversion (this one trivial).  I take it, though, that
execution of this fragment, with its implicit conversions,  is
entirely legal.

What precisely are the operations that "dirty" a pointer so that it is
no longer a "pointer returned by new"?  I am not interested in
implementation-based definitions; I can easily understand the
circumstances under which obvious implementations of delete will
"work."  I am interested in the official answer of the Standard.

P. Hilfinger