Topic: Deleting null pointers
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1996/11/02 Raw View
Hello all,
In his column in Doctor Dobbs Journal, Al Stevens recently
wrote that deleting a pointer that's been set to NULL can change the
value of that pointer.
The April '95 draft of the standard says that deleting a pointer
that's been set to null "has no effect." I'm of the opinion that "has
no effect" does not allow changing the value of the pointer.
My questions are:
1) does the current draft of the standard still require that deleting
a pointer that's been set to null have no effect?
2) is changing the value of the pointer illegal if the requirement is
still there, even if changing the value of other pointers is legal?
--
Later,
Jerry.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@taumet.eng.sun.com (Steve Clamage)
Date: 1996/11/02 Raw View
In article 327a8f56jcoffin9899a2@news.rmi.net, jcoffin@taeus.com (Jerry Coffin) writes:
>Hello all,
> In his column in Doctor Dobbs Journal, Al Stevens recently
>wrote that deleting a pointer that's been set to NULL can change the
>value of that pointer.
>
>The April '95 draft of the standard says that deleting a pointer
>that's been set to null "has no effect." I'm of the opinion that "has
>no effect" does not allow changing the value of the pointer.
>
>My questions are:
>
>1) does the current draft of the standard still require that deleting
> a pointer that's been set to null have no effect?
Yes, in 5.3.5 "Delete".
>2) is changing the value of the pointer illegal if the requirement is
> still there, even if changing the value of other pointers is legal?
The ARM said the delete operator could change the value of a pointer.
The draft standard does not mention that possibility at all, not even
to deny it. We can quickly analyze all possible cases:
1. Invalid but non-null pointer: The results are undefined, meaning
anything can happen.
2. Non-null pointer of an incorrect type, or to an object not allocated
with the corresponding version of 'new', or to an object already
deallocated: The results are undefined.
3. Pointer to an object of an appropriate type properly allocated and
not yet deallocated: After the delete operation, the value of the
pointer is indeterminate. That means you are not even allowed to compare
its current value with its original value, because it doesn't need to
have a current value. (A tagged architecture might trap any attempt to
read the pointer, for example, so you literally can't look at it.)
I believe this is the origin of the "delete can change the pointer"
idea. Note, however, that the pointer expression 'expr' in
delete expr;
need not be an lvalue, in which case it can't be modified.
4. Null pointer: The draft says the delete operation has no effect.
In the absence of any language saying the operation can change the
value of the pointer, I would conclude the pointer must still be null
after the delete expression. (Continuing the discussion in #3, it is
always allowed to compare a null pointer to any valid pointer of the same
type, or to a null pointer constant. The "indeterminate" rule does not
apply, since nothing gets deleted.)
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: smeyers@teleport.com (Scott Meyers)
Date: 1996/11/07 Raw View
In article 327a8f56jcoffin9899a2@news.rmi.net, jcoffin@taeus.com
(Jerry Coffin) writes:
>Hello all,
> In his column in Doctor Dobbs Journal, Al Stevens recently
>wrote that deleting a pointer that's been set to NULL can change the
>value of that pointer.
>
>The April '95 draft of the standard says that deleting a pointer
>that's been set to null "has no effect." I'm of the opinion that "has
>no effect" does not allow changing the value of the pointer.
>
>My questions are:
>
>1) does the current draft of the standard still require that deleting
> a pointer that's been set to null have no effect?
>
>2) is changing the value of the pointer illegal if the requirement is
> still there, even if changing the value of other pointers is legal?
This is my fault, I suppose, because I'm the one who wrote to Al describing
how the DWP allowed deletion of the null pointer to change the value of the
pointer. I quoted him this passage from 5.3.5 of the January 1996 DWP:
4 It is unspecified whether the deletion of an object changes its value.
If the expression denoting the object in a delete-expression is a mod-
ifiable lvalue, any attempt to access its value after the deletion is
undefined (_basic.stc.dynamic.deallocation_).
However, this passage is completely missing from the September 1996 DWP,
while this is still present:
2 If the operand has a class type, the operand is converted to a pointer
type by calling the above-mentioned conversion function, and the con-
verted operand is used in place of the original operand for the
remainder of this section. In either alternative, if the value of the
operand of delete is the null pointer the operation has no effect.
So now it looks like deleting the null pointer is not allowed to change the
value of the pointer. Ah, the joys of changing standards. Of course,
that's why they call them *drafts*.
Scott
--
Scott Meyers, Ph.D. Voice: 503/638-6028
C++ Consulting and Training Fax: 503/638-6614
Author of "Effective C++" Email: smeyers@netcom.com
and "More Effective C++" WWW: http://www.teleport.com/~smeyers
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]