Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Typecasting function pointers to void *

Reply
Thread Tools

Typecasting function pointers to void *

 
 
bnoordhuis@gmail.com
Guest
Posts: n/a
 
      07-15-2005
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'.

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      07-15-2005
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
 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      07-15-2005
In article < .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
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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-15-2005
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typecasting function pointer to void* WittyGuy C++ 5 07-27-2006 05:22 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57