Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Casting pointers to functions of different types

Reply
Thread Tools

Casting pointers to functions of different types

 
 
Yevgen Muntyan
Guest
Posts: n/a
 
      10-06-2006
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
 
Reply With Quote
 
 
 
 
Yevgen Muntyan
Guest
Posts: n/a
 
      10-06-2006
Yevgen Muntyan wrote:
> 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
}
 
Reply With Quote
 
 
 
 
straywithsmile@gmail.com
Guest
Posts: n/a
 
      10-06-2006
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!
Yevgen Muntyan wrote:
> 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


 
Reply With Quote
 
Yevgen Muntyan
Guest
Posts: n/a
 
      10-06-2006
Yevgen Muntyan wrote:
> 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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-06-2006
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      10-23-2006
On Fri, 06 Oct 2006 04:26:51 +0000, Richard Heathfield
<(E-Mail Removed)> wrote:

> Yevgen Muntyan said:


> > 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. <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
 
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
Do not cast pointers to functions to pointers to primitive types ajaybgr C Programming 18 09-07-2012 04:34 AM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Casting pointers to varargs functions Gene C Programming 6 11-03-2007 02:45 PM
Casting from pointer types to non-pointer types jois.de.vivre@gmail.com C++ 11 06-07-2007 09:17 AM
Functions taking pointers to different types as arguments Mikhail Teterin C Programming 9 12-24-2004 07:39 AM



Advertisments