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

Typecasting function pointers to void *

Consider this:

int foo(int *a, int *b);
int (*bar)(void *, void *) = (void *)foo;

How legal - or illegal - is the typecast and are there real-world
situations where such code will cause trouble? I don't mean trouble
like in 'not compiling' but trouble like in 'crashing hard'.

Nov 15 '05 #1
3 3961
bn********@gmail.com wrote:

Consider this:

int foo(int *a, int *b); (void *)foo; How legal - or illegal - is the typecast and are there real-world
situations where such code will cause trouble? I don't mean trouble
like in 'not compiling' but trouble like in 'crashing hard'.


Converting a pointer to a function,
to a pointer to type void, is undefined.

--
pete
Nov 15 '05 #2
In article <11**********************@g47g2000cwa.googlegroups .com>,
<bn********@gmail.com> wrote:
Consider this:

int foo(int *a, int *b);
int (*bar)(void *, void *) = (void *)foo;

How legal - or illegal - is the typecast and are there real-world
situations where such code will cause trouble? I don't mean trouble
like in 'not compiling' but trouble like in 'crashing hard'.


Conversions between function pointers and object pointers (void * is a
special case of "object pointers") is undefined.
I'd expect to see problems on systems with segmented memory spaces -
code and data segments are the first obvious way of using features like
segmentation, and the conversions between pointers into the different
segments may or may not be lossy (and there's no requirement that the
implementation fake lossless conversions if the underlying conversion
is lossy like there would be with void * and object pointers).

Any function pointer can be converted into any other function pointer
type and back to its original type without losing necessary information,
so if you need a "generic function pointer type" casting to something like
void (*)(void) and back will work. Note that this doesn't have the "able
to convert without a cast" feature that void * has for object pointers.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
There's a certain glee I get in the cockles of my heart every time I'm
reminded that there is no Flash plugin for my current platform.
--Eric Schwartz in the scary devil monastery
Nov 15 '05 #3
bn********@gmail.com wrote on 15/07/05 :
Consider this:

int foo(int *a, int *b);
int (*bar)(void *, void *) = (void *)foo;

How legal - or illegal - is the typecast and are there real-world
situations where such code will cause trouble? I don't mean trouble
like in 'not compiling' but trouble like in 'crashing hard'.


The behaviour is undefined.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Street | last post by:
Hi! I'm rather new at c++ and I'm totally confused with this kind of typecasting: typedef signed char int8_t; typedef signed short int16_t; typedef struct broadcast_hdr {
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
13
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
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...
12
by: srinivas.satish | last post by:
Hi, is it possible to typecast a function pointer to two different prototypes. eg., typedef void (functptr1 *) (int , int); typedef void (functptr2 *) (int); functptr1 fptr; fptr =...
4
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response)...
5
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
3
by: sritejv | last post by:
Hello everyone, I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work. void *xyz; struct abcd...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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...
0
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...
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.