Re: Cannot understand the following codes
- From: Ben C <spamspam@xxxxxxxxx>
- Date: 29 Mar 2006 07:52:09 GMT
On 2006-03-28, Alf P. Steinbach <alfps@xxxxxxxx> wrote:
* Ben C:
On 2006-03-28, Alf P. Steinbach <alfps@xxxxxxxx> wrote:
It's probably a void(*)(). But not matter what type it has, the cast is
undefined behavior in C++, and I believe also in C.
Why's it undefined behaviour? I can see that it invites undefined
behaviour, but not that it entails it-- if the value of
stGOFuncs.p_go_Measurements is a function of the correct type won't it
be OK?
It will be OK, /if/ the type is some pointer to function (but not if the
type is simply void*, in which case it's UB); I was too hasty there.
You're right, void * and function pointers are not compatible. I tried
this:
#include <stdio.h>
int f(int x)
{
return x * 2;
}
int main(void)
{
int (*q)(int) = f;
void *p = q; /* WARNING */
int (*r)(int) = p; /* WARNING */
int x = (*r)(3);
printf("Answer is %d\n", x);
return 0;
}
I had assumed void * was good for anything. But it's only good for data,
not functions.
I suppose on some machines functions might have a completely different
address space, be a completely different size etc., and the standard is
allowing for that...
Sorry about that... ;-)
Not at all, thank you, I was missing something.
.
- Follow-Ups:
- Re: Cannot understand the following codes
- From: Joe Wright
- Re: Cannot understand the following codes
- References:
- Cannot understand the following codes
- From: gpsabove
- Re: Cannot understand the following codes
- From: Alf P. Steinbach
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- From: Alf P. Steinbach
- Cannot understand the following codes
- Prev by Date: Re: String concatenation design
- Next by Date: Re: String concatenation design
- Previous by thread: Re: Cannot understand the following codes
- Next by thread: Re: Cannot understand the following codes
- Index(es):
Relevant Pages
|