473,847 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::s tring, 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 9541

"Damon" <so********@exc ite.com> wrote in message news:15******** *************** **@posting.goog le.com...
]

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********@exc ite.com> wrote in message news:15******** *************** **@posting.goog le.com...
]

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********@exci te.com (Damon) wrote:
Hi,

I created a template class and stored its instances in a map object
that is like std::map<std::s tring, 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::s tring, 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********@exc ite.com> wrote in message
news:15******** *************** **@posting.goog le.com...
Hi,

I created a template class and stored its instances in a map object
that is like std::map<std::s tring, 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_typei d)

HTH

Jul 22 '05 #5
In article <10************ ***@news.minx.n et.uk>,
Jumbo <pc************ ****@uko2.co.uk > wrote:
"Damon" <so********@exc ite.com> wrote in message
news:15******* *************** ***@posting.goo gle.com...
I created a template class and stored its instances in a map object
that is like std::map<std::s tring, 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_type id)


He might be able to "record" typeid's but as per the other responses
there are more "programmat ic" 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********@exci te.com (Damon) wrote in message news:<15******* *************** ***@posting.goo gle.com>...
Hi,

I created a template class and stored its instances in a map object
that is like std::map<std::s tring, 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
2042
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). You can find a problem there, which cannot be solved by dynamic_cast, because the pointers can be NULL, and I only want to know, if the pointer type is derived from another pointer type. BTW: I cannot create a temporary instance, because used...
13
2346
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
3743
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 }; void call_unknown_function( void *function, enum Type retType, void *retAddress, ...);
16
4466
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) ({ if(x && *x) { free(*x); *x=NULL; } }) void somefunction() { char *x = malloc(10); int *y = malloc(10);
19
2347
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 each C type dynamicly allocated. -- vir
26
3144
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 code will not be idiomatic C. GCC has an extension to ISO C that permits nested functions: <http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html> For implementing closures they have a serious limitation:
16
10414
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 do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
1
3287
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 -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal? Is what Keith Thompson said in another post also helpful to understand this question: "... The implicit conversion rule applies only to void*, not...
16
2706
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 allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
9886
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10338
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7882
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4119
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.