casting function pointers ?

A

Alfonso Morra

I have come accross some code where function pointers are being cast
from one type to another (admittedly, they all have the same return
type). eg. you may have func ptrs declr as ff:

typedef void (*vfptr)(void) ;
typedef void (*vfptr_i)(int) ;
typedef void (*vfptr_ii)(int, int ) ;
typedef void (*vfptr_iis)(int, int, char*) ;

the first declaration is used like a kind of base class functor in the
sense that sometimes, the other classes are cast "down" to it. In these
cases, the func args are determined at run time - typically, retrieved
from global variables etc.

This seems quite reckless. I have never seen anything like it before.
Has anyone come accros something like this before?
 
G

Giorgos Keramidas

Alfonso Morra said:
I have come accross some code where function pointers are being cast
from one type to another (admittedly, they all have the same return
type). eg. you may have func ptrs declr as ff:

typedef void (*vfptr)(void) ;
typedef void (*vfptr_i)(int) ;
typedef void (*vfptr_ii)(int, int ) ;
typedef void (*vfptr_iis)(int, int, char*) ;

the first declaration is used like a kind of base class functor in the
sense that sometimes, the other classes are cast "down" to it. In these
cases, the func args are determined at run time - typically, retrieved
from global variables etc.

This seems quite reckless. I have never seen anything like it
before. Has anyone come accros something like this before?

I guess whoever wrote these casts doesn't care too much about weird
stack problems :)
 
E

Eric Sosman

Alfonso said:
I have come accross some code where function pointers are being cast
from one type to another (admittedly, they all have the same return
type). eg. you may have func ptrs declr as ff:

typedef void (*vfptr)(void) ;
typedef void (*vfptr_i)(int) ;
typedef void (*vfptr_ii)(int, int ) ;
typedef void (*vfptr_iis)(int, int, char*) ;

the first declaration is used like a kind of base class functor in the
sense that sometimes, the other classes are cast "down" to it. In these
cases, the func args are determined at run time - typically, retrieved
from global variables etc.

This seems quite reckless. I have never seen anything like it before.
Has anyone come accros something like this before?

It's ugly, but far from reckless and sometimes necessary.

The Standard says that it's possible to convert a function
pointer from one type to another without harm, so the casts
using the types above are all right. The Standard also says,
though, that when a call is made the type of the function
pointer must agree with that of the called function, or else
you get undefined behavior. So,

void func_ii(int, int);
vfptr fp = (vfptr)func_ii; /* all is well */
fp(1, 2); /* compile-time error */
fp(); /* undefined behavior */
((vfptr_ii)fp)(1, 2); /* all is well */

You usually find this sort of thing in connection with a
table of function pointers accompanied by type codes of some
kind. They'll be used more or less like this

vfptr fp = table.funcptr;
switch (table.functype) {
case V:
fp();
break;
case I:
((vfptr_i)fp)(1);
break;
case II:
((vfptr_ii)fp)(1, 2);
break;
case IIS:
((vfptr_iis)fp)(1, 2, "three");
break;
...
default:
fprintf (stderr, "BUG: unknown type code %d\n",
table.functype);
die_horribly();
}

.... to allow the program to make a run-time choice among many
functions with different "signatures."
 
A

Alfonso Morra

Eric said:
Alfonso said:
I have come accross some code where function pointers are being cast
from one type to another (admittedly, they all have the same return
type). eg. you may have func ptrs declr as ff:

typedef void (*vfptr)(void) ;
typedef void (*vfptr_i)(int) ;
typedef void (*vfptr_ii)(int, int ) ;
typedef void (*vfptr_iis)(int, int, char*) ;

the first declaration is used like a kind of base class functor in the
sense that sometimes, the other classes are cast "down" to it. In
these cases, the func args are determined at run time - typically,
retrieved from global variables etc.

This seems quite reckless. I have never seen anything like it before.
Has anyone come accros something like this before?


It's ugly, but far from reckless and sometimes necessary.

The Standard says that it's possible to convert a function
pointer from one type to another without harm, so the casts
using the types above are all right. The Standard also says,
though, that when a call is made the type of the function
pointer must agree with that of the called function, or else
you get undefined behavior. So,

void func_ii(int, int);
vfptr fp = (vfptr)func_ii; /* all is well */
fp(1, 2); /* compile-time error */
fp(); /* undefined behavior */
((vfptr_ii)fp)(1, 2); /* all is well */

You usually find this sort of thing in connection with a
table of function pointers accompanied by type codes of some
kind. They'll be used more or less like this

vfptr fp = table.funcptr;
switch (table.functype) {
case V:
fp();
break;
case I:
((vfptr_i)fp)(1);
break;
case II:
((vfptr_ii)fp)(1, 2);
break;
case IIS:
((vfptr_iis)fp)(1, 2, "three");
break;
...
default:
fprintf (stderr, "BUG: unknown type code %d\n",
table.functype);
die_horribly();
}

... to allow the program to make a run-time choice among many
functions with different "signatures."



Thanks for the clarification - this sure is an eye opener.
 
B

Barry Schwarz

On Fri, 29 Jul 2005 14:41:38 +0000 (UTC), Alfonso Morra

snip 70 + lines
Thanks for the clarification - this sure is an eye opener.

Please trim your posts. You do not need to quote everything just to
say thanks.


<<Remove the del for email>>
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top