468,514 Members | 1,695 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

Conversion from void* to a point of a class

I want to know to the following in a safe manner (the following code
will be dangerous if p does not contain a pointer to MyClass). I would
like someone to show me a better way (either throw an exception or
return value which I can check if the cast is allowed or not.

void* p;
...
MyClass* pClass = (MyClass*)p;

Thank you very much for your help.

Charles Zhang
Dec 11 '07 #1
7 1158
Hi Charles,
>void* p;
...
MyClass* pClass = (MyClass*)p;
>Thank you very much for your help.

initialize p == NULL, and check whether
p has some valid address pointing at before
you do the casting. NEVER use uninitialized
variables like you wrote here! They will point
to somewhere but not to your allocated memory
for your Class in this example!

something like:

void *p == NULL;

.....
.....
.....

if(p != NULL){
MyClass* pClass = (MyClass*)p;
}else{
//some invalid pointer handling code here
//a messagebox or some console output or whatever
//you like to handle this situation
}
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 11 '07 #2
(MyClass*) is a C-style cast - in C++, reinterpret_cast, static_cast and
dynamic_cast are preferred. If you are not sure whether p really points
to a MyClass object or rather to an object of a different class, use
dynamic_cast:
http://msdn2.microsoft.com/en-us/lib...cs(VS.80).aspx

--Johannes

Charles Zhang wrote:
I want to know to the following in a safe manner (the following code
will be dangerous if p does not contain a pointer to MyClass). I would
like someone to show me a better way (either throw an exception or
return value which I can check if the cast is allowed or not.

void* p;
...
MyClass* pClass = (MyClass*)p;

Thank you very much for your help.

Charles Zhang

--
Johannes Passing - http://int3.de/
Dec 11 '07 #3
Of course, I will make sure the void pointer is not NULL. However, a
non-null pointer is not necessary a valid pointer of MyClass. Your code
will cause a run time error which could crash the application.

Charles Zhang


Kerem Gümrükcü wrote:
Hi Charles,
>void* p;
...
MyClass* pClass = (MyClass*)p;
>Thank you very much for your help.


initialize p == NULL, and check whether
p has some valid address pointing at before
you do the casting. NEVER use uninitialized
variables like you wrote here! They will point
to somewhere but not to your allocated memory
for your Class in this example!

something like:

void *p == NULL;

....
....
....

if(p != NULL){
MyClass* pClass = (MyClass*)p;
}else{
//some invalid pointer handling code here
//a messagebox or some console output or whatever
//you like to handle this situation
}
Regards

Kerem
Dec 11 '07 #4
Thank you very much. But I actually tried to use dynamic_cast, but I got
an compiler error

void* p = NULL;
....
....
MyClass* pClass = dynamic_cast<MyClass*>(p);

The compiler error is: "invalid expression type for dynamic_cast."

Then I changed the line to:
MyClass* pClass = dynamic_cast<MyClass*>((MyClass*)p);

Now, it compiles. But when p is not an valid pointer to MyClass, now run
time exception was thrown. Which could cause the application to crash.

Charles Zhang

Johannes Passing wrote:
(MyClass*) is a C-style cast - in C++, reinterpret_cast, static_cast and
dynamic_cast are preferred. If you are not sure whether p really points
to a MyClass object or rather to an object of a different class, use
dynamic_cast:
http://msdn2.microsoft.com/en-us/lib...cs(VS.80).aspx

--Johannes

Charles Zhang wrote:
>I want to know to the following in a safe manner (the following code
will be dangerous if p does not contain a pointer to MyClass). I would
like someone to show me a better way (either throw an exception or
return value which I can check if the cast is allowed or not.

void* p;
...
MyClass* pClass = (MyClass*)p;

Thank you very much for your help.

Charles Zhang



Dec 11 '07 #5
dynamic_cast requires a polymorphic type: p must point to an object with at
least one virtual function. Usually this kind of mistake is caught at compile
time, but due to indirection (p) compiler is not be able to do that (and of
course, an invalid pointer is not a polymorphic type).

Regards
--
Cholo Lennon
Bs.As.
ARG
"Charles Zhang" <Ch**********@newsgroups.nospamwrote in message
news:#Z**************@TK2MSFTNGP04.phx.gbl...
Thank you very much. But I actually tried to use dynamic_cast, but I got
an compiler error

void* p = NULL;
...
...
MyClass* pClass = dynamic_cast<MyClass*>(p);

The compiler error is: "invalid expression type for dynamic_cast."

Then I changed the line to:
MyClass* pClass = dynamic_cast<MyClass*>((MyClass*)p);

Now, it compiles. But when p is not an valid pointer to MyClass, now run
time exception was thrown. Which could cause the application to crash.

Charles Zhang

Johannes Passing wrote:
(MyClass*) is a C-style cast - in C++, reinterpret_cast, static_cast and
dynamic_cast are preferred. If you are not sure whether p really points
to a MyClass object or rather to an object of a different class, use
dynamic_cast:
http://msdn2.microsoft.com/en-us/lib...cs(VS.80).aspx

--Johannes

Charles Zhang wrote:
I want to know to the following in a safe manner (the following code
will be dangerous if p does not contain a pointer to MyClass). I would
like someone to show me a better way (either throw an exception or
return value which I can check if the cast is allowed or not.

void* p;
...
MyClass* pClass = (MyClass*)p;

Thank you very much for your help.

Charles Zhang



Dec 11 '07 #6
Well, that is true, dynamic_cast does not work for void-pointers - once
you have converted your MyClass* pointer to a void* you have voluntarily
given up all type information and there is no safe way back.

To convert a void pointer back to MyClass*, you have to use
reinterpret_cast, which in this context is basically the same as the
C-style cast you used initially. There is, of course, no guarantee that
the object returned by reinterpret_cast is indeed a valid object of
MyClass or rather some other garbage.

If possible by any measure, avoid using void-pointers in C++ in the
first place - there is almost always a better way to do it. In C, where
no such thing like RTTI exists, there is one common idiom you could use
in C++ as well if you are urged to use void pointers: To provide at
least a certain degree of sanity checking before using a casted pointer,
include some signature/magic field in the struct/class and check that it
contains the expected value before using it:

void *p = ...
MyClass *my = reinterpret_cast<MyClass*>(p);
if ( my->Signature != MYCLASS_SIGNATURE )
{
// this pointer is invalid
}
else
{
// it is likely the the pointer is valid
}

If you can avoid using void-pointers, do that - it may save you a lot of
trouble.

--Johannes

Charles Zhang wrote:
Thank you very much. But I actually tried to use dynamic_cast, but I got
an compiler error

void* p = NULL;
...
...
MyClass* pClass = dynamic_cast<MyClass*>(p);

The compiler error is: "invalid expression type for dynamic_cast."

Then I changed the line to:
MyClass* pClass = dynamic_cast<MyClass*>((MyClass*)p);

Now, it compiles. But when p is not an valid pointer to MyClass, now run
time exception was thrown. Which could cause the application to crash.

Charles Zhang

Johannes Passing wrote:
>(MyClass*) is a C-style cast - in C++, reinterpret_cast, static_cast
and dynamic_cast are preferred. If you are not sure whether p really
points to a MyClass object or rather to an object of a different
class, use dynamic_cast:
http://msdn2.microsoft.com/en-us/lib...cs(VS.80).aspx

--Johannes

Charles Zhang wrote:
>>I want to know to the following in a safe manner (the following code
will be dangerous if p does not contain a pointer to MyClass). I
would like someone to show me a better way (either throw an exception
or return value which I can check if the cast is allowed or not.

void* p;
...
MyClass* pClass = (MyClass*)p;

Thank you very much for your help.

Charles Zhang




--
Johannes Passing - http://int3.de/
Dec 11 '07 #7
Johannes Passing wrote:
Well, that is true, dynamic_cast does not work for void-pointers - once
you have converted your MyClass* pointer to a void* you have voluntarily
given up all type information and there is no safe way back.

To convert a void pointer back to MyClass*, you have to use
reinterpret_cast, which in this context is basically the same as the
C-style cast you used initially.
Johannes:

You can (and should) use static_cast to convert void pointer. Using
reinterpret_cast is usually an indication that you are doing something
non-portable (which casting a void pointer usually is not).

--
David Wilkinson
Visual C++ MVP
Dec 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Thomas Matthews | last post: by
3 posts views Thread by Siemel Naran | last post: by
31 posts views Thread by Bjørn Augestad | last post: by
2 posts views Thread by Alex Sedow | last post: by
9 posts views Thread by Girish | last post: by
1 post views Thread by tsar.omen | last post: by
2 posts views Thread by xtrigger303 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.