473,289 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

dynamic_cast to find out the deleted pointer

On devx site, I saw following code. It says when a derived class is
tried to cast to base type, it looks at the missing vtable and
complains if the object is already deleted.
I am of the opinion that this doesnt work if the destructor is not
virtual or when the class has no virtual members. I would like to know
is there anything wrong in what I am thinking.
(I agree it is better to keep base class dtor as virtual, but supposing
it is not virtual, does following method work to detect a deleted
pointer ?)

Thanks
Ganesh

#include <iostream>

using namespace std;

class A
{
public:
A() {}
virtual ~A() {}
};

class B : public A
{
public:
B() {}
};

int main()
{
B* pB = new B;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;
delete pB;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;
}

the output:
dynamic_cast<B*>( pB) worked
dynamic_cast<B*>( (A*)pB) worked
dynamic_cast<B*>( pB) worked
dynamic_cast<B*>( (A*)pB) failed

Jul 23 '05 #1
3 2515
Ganesh wrote:
....
(I agree it is better to keep base class dtor as virtual, but supposing
it is not virtual, does following method work to detect a deleted
pointer ?)
Nothing knows what a deleted pointer is.

.... B* pB = new B;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;
delete pB;
Everything referencing pB after this point is undefined behaviour.
You're lucky it does not melt your CPU.

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;

....
Jul 23 '05 #2

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:65********************@speakeasy.net...
Ganesh wrote:
...
(I agree it is better to keep base class dtor as virtual, but supposing
it is not virtual, does following method work to detect a deleted
pointer ?)


Nothing knows what a deleted pointer is.

...
B* pB = new B;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;
delete pB;


Everything referencing pB after this point is undefined behaviour. You're
lucky it does not melt your CPU.


A wee bit dramatic there, wouldn't you say? I should hope that undefined
behavior doesn't lead to anything quite *that* drastic! If it does, I'd
better do a LOT more work on validating my code before I run any unit tests!
And can you imagine the tech support calls? "I found a bug in your new
software... could you send someone over to put out the fire, please?" :-)
-Howard


Jul 23 '05 #3
Howard wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message

Everything referencing pB after this point is undefined behaviour. You're
lucky it does not melt your CPU.

A wee bit dramatic there, wouldn't you say? I should hope that undefined
behavior doesn't lead to anything quite *that* drastic! If it does, I'd
better do a LOT more work on validating my code before I run any unit tests!
And can you imagine the tech support calls? "I found a bug in your new
software... could you send someone over to put out the fire, please?" :-)


Yeah, he's dramatic, but technically, Gianni is correct. Once you
invoke UB, anything *can* happen. It's highly unlikely that some of the
possible effects (such as causing the sun to go nova, turning lead into
gold, etc...) will happen, but the standard does not forbid them :)
Jul 23 '05 #4

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

Similar topics

3
by: alg | last post by:
dynamic_cast<> comes in play when to perform conversion from a pointer to a base class to a pointer to a derived class. I don't understand: 1. why this is so necessary since we can either use...
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As...
1
by: Steven T. Hatton | last post by:
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...
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
7
by: wangtianthu | last post by:
I am writing a program using some RPC infrastructure. there is code like this: MyServer* server = dynamic_cast<MyServer*>(GetServerByName(MY_SERVER)); Here GetServerByName() will return a...
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
8
by: pietromas | last post by:
In the example below, why does the dynamic_cast fail (return NULL)? It should be able to cast between sibling classes ... #include <iostream> class A { public: virtual const int get()...
13
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.