473,748 Members | 4,030 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 9531

"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
2038
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
2340
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
3733
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
4452
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
2330
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
3130
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
10392
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
3281
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
2695
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
8991
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
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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
6796
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
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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

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.