Casting pointers to functions of different types

Y

Yevgen Muntyan

Hey,

Is the following code valid? It has to cast a function pointer to some
fixed type to allow api for storing and calling that function, but is
such cast legal? Code which stored function pointer in array of unsigned
chars would be valid (I guess), but would be lot uglier than this.

typedef void (*AFunc) (void);
typedef void (*ConcreteFunc) (int);

void some_func (int)
{
}

void marshal (AFunc func, int *args)
{
ConcreteFunc cfunc = func;
cfunc (args[0]);
}

int main (void)
{
int arg = 1;
AFunc func = some_func; // here we cast ConcreteFunc to AFunc
marshal (func, &arg, 1); // and then marshal() casts it back
}

Thanks,
Yevgen
 
Y

Yevgen Muntyan

Yevgen said:
Hey,

Is the following code valid?

Here's compilable code, sorry:

typedef void (*AFunc) (void);
typedef void (*ConcreteFunc) (int);

void some_func (int arg)
{
}

void marshal (AFunc func, int *args)
{
ConcreteFunc cfunc = (ConcreteFunc) func;
cfunc (args[0]);
}

int main (void)
{
int arg = 1;
AFunc func = (AFunc) some_func;// here we cast ConcreteFunc to AFunc
marshal (func, &arg); // and then marshal() casts it back
}
 
S

straywithsmile

I never think this can work, you'd better check on your compiler,but I
think 99% chance, you will receive an error message from the compiler.
and if it is C++, you must wrong.
If your compiler is old version, you may test a function with no
parameter, that means, it will not check the parameter, maybe it can
work. Good Luck!
 
Y

Yevgen Muntyan

Yevgen said:
Hey,

Is the following code valid?

Nevermind, standard explicitely states it is, except errors
not related to casts :)

"A pointer to a function of one type may be converted to a pointer to a
function of another type and back again; the result shall compare equal
to the original pointer."

Regards,
Yevgen
 
R

Richard Heathfield

Yevgen Muntyan said:
Hey,

Is the following code valid?

No comment! :) I'm going to answer the question, if I may, rather than the
code.
It has to cast a function pointer to some
fixed type to allow api for storing and calling that function, but is
such cast legal?

Yes. All function pointer types are guaranteed to have the same
representation, so it's okay to store any function pointer value in any
function pointer object. BUT there are no implicit conversions, like with
void * for objects. So unless you know the RHS is exactly the proper type
for the LHS, you must add a cast to get it to the right type. This is
another of those very few places where a cast is necessary and therefore
worth having.
 
D

Dave Thompson

Yevgen Muntyan said:

Yes. All function pointer types are guaranteed to have the same
representation, so it's okay to store any function pointer value in any
function pointer object. <snip>

They aren't specifically required to have the same representation. It
is required that you can convert any function pointer type to any
other and back without loss of information, and I have never heard of
or even imagined any machine where it would make sense to accomplish
this by any means other than using the same representation.

But what the OP was doing (or trying to) was converting, and that is
specifically guaranteed.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top