469,603 Members | 2,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

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 3790
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Street | last post: by
11 posts views Thread by Vinod Patel | last post: by
13 posts views Thread by brian | last post: by
12 posts views Thread by srinivas.satish | last post: by
4 posts views Thread by vivekian | last post: by
5 posts views Thread by WittyGuy | last post: by
12 posts views Thread by bwaichu | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.