473,657 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic_cast<T* >(deleted_point er)?

The result shown below doesn't surprise me now. But it did several months
ago when I followed some bad advice and tried to check if I had a live
object at the address referenced by a pointer. Can I assume the result is
consistente with the Standard?

Tue May 10 21:24:24:> cat foo.cc
#include <iostream>
struct Foo{virtual ~Foo(){} };
struct Bar{virtual ~Bar(){} };
int main(){
Foo* foo = new Foo;
Bar* bar = new Bar;

std::cout << "dynamic_cast<F oo*>(foo)"
<< dynamic_cast<Fo o*>(foo)
<< std::endl;

delete foo;
std::cout << "dynamic_cast<F oo*>(foo)"
<< dynamic_cast<Fo o*>(foo)
<< std::endl;

std::cout << "dynamic_cast<F oo*>(bar)"
<< dynamic_cast<Fo o*>(bar)
<< std::endl;
}

Tue May 10 21:25:45:> ./foo
dynamic_cast<Fo o*>(foo)0x804a0 08
dynamic_cast<Fo o*>(foo)0x804a0 08
dynamic_cast<Fo o*>(bar)0

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 23 '05 #1
1 1520
"Steven T. Hatton" <ch********@ger mania.sup> wrote in message
news:Aq******** ************@sp eakeasy.net...
The result shown below doesn't surprise me now. But it did several months
ago when I followed some bad advice and tried to check if I had a live
object at the address referenced by a pointer. Can I assume the result is
consistente with the Standard?

Tue May 10 21:24:24:> cat foo.cc
#include <iostream>
struct Foo{virtual ~Foo(){} };
struct Bar{virtual ~Bar(){} };
int main(){
Foo* foo = new Foo;
Bar* bar = new Bar;

std::cout << "dynamic_cast<F oo*>(foo)"
<< dynamic_cast<Fo o*>(foo)
<< std::endl;

delete foo;
All bets are off once you delete it.
std::cout << "dynamic_cast<F oo*>(foo)"
<< dynamic_cast<Fo o*>(foo)
<< std::endl;
Anything can happen above - the "right" answer, the "wrong" answer, a crash,
a nuclear explosion, or anything else.
std::cout << "dynamic_cast<F oo*>(bar)"
<< dynamic_cast<Fo o*>(bar)
<< std::endl;
}

Tue May 10 21:25:45:> ./foo
dynamic_cast<Fo o*>(foo)0x804a0 08
dynamic_cast<Fo o*>(foo)0x804a0 08
Probably the deleted memory still hasn't been altered following the delete.
But you can't count on that.
dynamic_cast<Fo o*>(bar)0


DW
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.