Can I check the type of a void pointer

D

Damon

Hi,

I created a template class and stored its instances in a map object
that is like std::map<std::string, void* >. But i just made a nasty
discovery that if i fail to use a C-style cast properly with the void
pointer to return it to the proper class it belongs to, I get very bad
and random segmentation fault.

So question to all the experts, can I check the type of a void pointer
or use some guaranteed way to elicit an exception without going
segmentation fault? BTW, I'm using g++ v3.2.2. I know that if i try to
delete the instances of the template class that was cast wrongly, it
segfault but I can't trap it!

Thank you in advance! Merry X'mas to everyone.

Regards,
Damon
 
R

Ron Natalie

Damon said:
So question to all the experts, can I check the type of a void pointer
or use some guaranteed way to elicit an exception without going
segmentation fault? BTW, I'm using g++ v3.2.2. I know that if i try to
delete the instances of the template class that was cast wrongly, it
segfault but I can't trap it!
No, once you cast a pointer to void*, you're only options are:
check it against the null pointer constant.
cast it back to exactly what it was before.

The only solution I can suggest is to make your template class inherit
from a common (public) base class and use that rather than void*. If
the base class is polymorphic (has a virutal function), then you can use
dynamic_cast to validate it against any particular derived class.
 
M

Martijn Lievaart

No, once you cast a pointer to void*, you're only options are:
check it against the null pointer constant.
cast it back to exactly what it was before.

The only solution I can suggest is to make your template class inherit
from a common (public) base class and use that rather than void*. If
the base class is polymorphic (has a virutal function), then you can use
dynamic_cast to validate it against any particular derived class.

Adding to that, if you /need/ a void pointer (say for some C-ish
callback), you can use this technique as well. Cast to the base class.
Cast to void. Pass pointer. Cast back to base class. Use polymorphism
(virtual functions) or dynamic_cast<> from there.

HTH,
M4
 
T

tom_usenet

Hi,

I created a template class and stored its instances in a map object
that is like std::map<std::string, void* >. But i just made a nasty
discovery that if i fail to use a C-style cast properly with the void
pointer to return it to the proper class it belongs to, I get very bad
and random segmentation fault.

So question to all the experts, can I check the type of a void pointer
or use some guaranteed way to elicit an exception without going
segmentation fault? BTW, I'm using g++ v3.2.2. I know that if i try to
delete the instances of the template class that was cast wrongly, it
segfault but I can't trap it!

A void* doesn't include any info about the type of the object pointed
to. A boost::any does: std::map<std::string, boost::any>. See
www.boost.org. Alternatively, redesign and use something better than a
void* (such as a base class with virtual functions). There's rarely
any good reason to use something like boost::any.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
J

Jumbo

Damon said:
Hi,

I created a template class and stored its instances in a map object
that is like std::map<std::string, void* >. But i just made a nasty
discovery that if i fail to use a C-style cast properly with the void
pointer to return it to the proper class it belongs to, I get very bad
and random segmentation fault.

So question to all the experts, can I check the type of a void pointer
or use some guaranteed way to elicit an exception without going
segmentation fault? BTW, I'm using g++ v3.2.2. I know that if i try to
delete the instances of the template class that was cast wrongly, it
segfault but I can't trap it!

Thank you in advance! Merry X'mas to everyone.
Your a bit early aren't you? :eek:) Merry Christmas to you too.

Have you tried STL <typeinfo>
catch(bad_typeid)

HTH
 
G

Greg Comeau

Your a bit early aren't you? :eek:) Merry Christmas to you too.

Have you tried STL <typeinfo>
catch(bad_typeid)

He might be able to "record" typeid's but as per the other responses
there are more "programmatic" ways to approach the problem, and
they will naturally adapt automatically for him too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top