473,407 Members | 2,320 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,407 software developers and data experts.

Can I check the type of a void pointer

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
6 9434

"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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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)....
13
by: herrcho | last post by:
int intcmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } in the above , if i put just 'void' instead of 'const void' as a parameter, what's the difference ?
4
by: Calum | last post by:
I need to call a function in a shared library, but the type of the function is not known until run-time. enum Type { Void=0, Char, Uchar, Short, Ushort, Int, Uint, Float, Double, String }; ...
16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
19
by: Victor Nazarov | last post by:
Consider I want to write my own version of standard malloc, calloc, realloc, free. How can I portably check if they work correctly? The most remarkable will be the test for right justification of...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.