Re: Some pointer quiestions again
- From: "Robert Gamble" <rgamble99@xxxxxxxxx>
- Date: 2 Jun 2005 11:06:22 -0700
Chris Torek wrote:
> In article <1117569067.744233.160900@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
> Robert Gamble <rgamble99@xxxxxxxxx> wrote:
> >Correct. A function pointer can be converted to a void pointer [1] but
> >cannot portably be converted back again. ...
>
> You can do the conversion (in either direction) with a cast:
>
> void f(void (*fp)(void)) {
> void *p = (void *)fp;
> ...
> }
>
> but the result is not necessarily useful. For instance, older x86
> implementations have models in which "char *"/"void *" is 32 bits
> long, while "void (*)(void)" is only 16 bits; and more models in
> which "char *"/"void *" is 16 bits long, but "void (*)(void)" is
> 32 bits. Clearly, in any system on which one conversion preserves
> all the bits (by going from 16 bits to 32 bits), the opposite
> conversion destroys some bits (by going from 32 to 16).
>
> Since both systems exist, both conversions destroy some bits on
> some systems. (That is, suppose System A has 16-bit function
> pointers and 32-bit data pointers; and System B has 32-bit function
> pointers and 16-bit data pointers. On System A, going from function
> to data preserves everything, but going from data to function wipes
> out bits. So the [1989] C standard could have attempted to mandate
> that "function to data preserves bits" -- but then System B loses
> out, because on System B, it is the function-to-data conversion
> that loses bits, while the data-to-function conversion preserves
> them.)
>
> >[1] I am not sure that a function pointer can even be converted to a
> >void pointer reliably, if not then there would appear to be no portable
> >way to print the value of a function pointer.
>
> This is indeed the case.
>
> I have seen a proposal for a "%P" (uppercase P) conversion to print
> a function pointer, to solve this problem. Of course, the output
> for "%p" (lowercase p) is already implementation-defined anyway, and
> printing "void *" data pointers is thus of limited usefuless. There
> seems to be no great clamor for printing function pointers.
Thank you very much for your detailed clarification. I had long
suspected that this was the case but hadn't given it too much thought.
Your explanation makes a lot of sense and clears this up for me.
Robert Gamble
.
- References:
- Re: Some pointer quiestions again
- From: Chris Torek
- Re: Some pointer quiestions again
- Prev by Date: Re: pointers and integers
- Next by Date: Re: Returning pointer to array problem
- Previous by thread: Re: Some pointer quiestions again
- Next by thread: Re: Some pointer quiestions again
- Index(es):
Relevant Pages
|