cast to pointer to function

E

etienne

Hello all,
i'm experimenting a problem in trying to cast a char * variable to a
pointer to a function.
My problem is that I want to execute functions in threads, using the
pthread_create function.
The name of the functions are read in txt file at runtime.
So I want to do something like
char * func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)funct_to_run, arg);

In fact, what I need is not exactly a cast, it is something that gets
the pointer to the function that match the name in func_to_run.
The functions are compiled and linked somewhere in the executable.

Even better I would like to test, at runtime, that the provided name
in func_to_run is the name of a function that exists.

Is there a way to do this?

thanks for any help

Etienne
 
C

Charles Bailey

Hello all,
i'm experimenting a problem in trying to cast a char * variable to a
pointer to a function.
My problem is that I want to execute functions in threads, using the
pthread_create function.
The name of the functions are read in txt file at runtime.
So I want to do something like
char * func_to_run = scanf(the, good, args);
pthread_create (thread, attr, (magic cast)funct_to_run, arg);

You can't do this, the cast would (in theory) be:
pthread_create(thread, attr, (void *(*)(void *))funct_to_run, arg)

This won't work because funct_to_run is not actually a pointer to a
function but just a pointer to the function name.
Even better I would like to test, at runtime, that the provided name
in func_to_run is the name of a function that exists.

I suggest that this is a bad idea, I don't think that you want every
function that exists in you program to be runnable by name from an
external source, how would you even get the arguments correct?

To do this you would have to use a platform specific solution such as
a dlopen/dlsym call. This makes the problem more complex (and OT).

There's probably a restricted set of functions with a standard
signature that are candidates for this type of execution. In this
case you could have a map of external name to function pointer, and
check this list when you are given the name of the function to call.

E.g.

#include <string.h>

struct func_pair
{
const char* fname;
void *(*fptr)(void *);
};

void *fn1(void*);
void *fn2(void*);
void *fn3(void*);
/* ... */
void *fnn(void*);

struct func_pair allowable_fns[] =
{
{ "fn1", fn1 },
{ "fn2", fn2 },
{ "fn3", fn3 },
/* ... */
{ "fnn", fnn },
};

/* This line shows why I should have used a typedef. */
void *(* get_fn(const char* name) )(void *)
{
int n;
void *(*fret)(void *) = 0;

for (n = 0;
n < sizeof(allowable_fns) / sizeof(allowable_fns[0]);
++n)
{
if (strcmp(name, allowable_fns[0].fname) == 0)
{
fret = allowable_fns[0].fptr;
break;
}
}
return fret;
}
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top