Re: Checking whether a pointer has been deleted
v04bvs@gmail.com wrote:
MyClass* p = new myClass();
delete p;
if (p) //behaviour is not defined
AFAIK behaviour is well defined.
18.4.1.1.3 says that new must return non-null pointer.
"delete p" don't affects to p, so p points to old non-null address.
The standard (?5.3.5/4) clearly says that after a delete
expression, the pointer value is invalid. Accessing an invalid
value is undefined behavior.
?18.4.1.1.3 doesn't say anything about the delete operator; it
defines the required semantics for the operator delete function,
which is only part of what the delete operator does. I don't
think it's completely clear whether the delete operator can
modify the bit pattern of a pointer, when its operand is an
lvalue, but it doesn't matter. Whether the bit pattern is
modified or not, the pointer is invalid.
check "if (p)" will always success.
probably after "delete p;" you have to write "p = 0;"
That doesn't really change anything. You can test if a pointer
is null, and in some cases, writing "p = NULL ;" makes sense,
but it only affects that one particular pointer, not other
pointers to the same element, and in the general case, is of no
use whatsoever.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]