By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Can I check the type of a void pointer

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Damon" <so********@excite.com> wrote in message news:15*************************@posting.google.co m...
]

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.
Jul 22 '05 #2

P: n/a
On Tue, 02 Dec 2003 09:57:28 -0500, Ron Natalie wrote:

"Damon" <so********@excite.com> wrote in message news:15*************************@posting.google.co m...
]

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.


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

Jul 22 '05 #3

P: n/a
On 2 Dec 2003 06:50:52 -0800, so********@excite.com (Damon) wrote:
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
Jul 22 '05 #4

P: n/a

"Damon" <so********@excite.com> wrote in message
news:15*************************@posting.google.co m...
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? :o) Merry Christmas to you too.

Have you tried STL <typeinfo>
catch(bad_typeid)

HTH

Jul 22 '05 #5

P: n/a
In article <10***************@news.minx.net.uk>,
Jumbo <pc****************@uko2.co.uk> wrote:
"Damon" <so********@excite.com> wrote in message
news:15*************************@posting.google.c om...
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? :o) 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.
--
Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #6

P: n/a
Thanks to all. It's an eye opener.

so********@excite.com (Damon) wrote in message news:<15*************************@posting.google.c om>...
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

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.