cast to pointer to function

Discussion in 'C Programming' started by etienne, Jul 19, 2007.

  1. etienne

    etienne Guest

    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
     
    etienne, Jul 19, 2007
    #1
    1. Advertising

  2. On 2007-07-19, etienne <> wrote:
    > 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;
    }
     
    Charles Bailey, Jul 19, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    733
    Ivan Vecerina
    Oct 12, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,117
    Dan Pop
    Feb 10, 2004
  3. Szabolcs Borsanyi
    Replies:
    6
    Views:
    684
    Ben Bacarisse
    May 23, 2008
  4. Replies:
    1
    Views:
    349
  5. Hansen
    Replies:
    3
    Views:
    1,152
    rep_movsd
    Apr 24, 2010
Loading...

Share This Page