cast to pointer to function

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

  1. etienne

    etienne Guest

    Hello,
    I am looking for help for a cast problem:
    I want to run functions in thread, using the pthread_create function.
    The function to run is the first argument of pthread_create and is
    expected to be of type void* (*func)(void*)
    My problem is that I want to get the name of the function to run in
    a .txt file that I parse at runtime.
    I read the file and I get the name in a char* variable.
    Now I want that this char* variable is the name of the function to
    run. How do I do that?
    I want to do something like:
    char *func_to_run = scanf(the, good, args);
    pthread_create (thread, attr, (magic cast)func_to_run, arg);

    Even better, is it possible to test, at run time, that the provided
    name is the name of a function that exists somewhere in the compiled
    code?

    Thanks for any help

    Etienne
     
    etienne, Jul 18, 2007
    #1
    1. Advertising

  2. etienne

    santosh Guest

    On Wednesday 18 Jul 2007 3:25 pm, in comp.lang.c, etienne
    <> wrote:
    Message ID: <>

    > Hello,
    > I am looking for help for a cast problem:
    > I want to run functions in thread, using the pthread_create
    > function. The function to run is the first argument of
    > pthread_create and is expected to be of type void* (*func)(void*)
    > My problem is that I want to get the name of the function to run in
    > a .txt file that I parse at runtime.
    > I read the file and I get the name in a char* variable.
    > Now I want that this char* variable is the name of the function to
    > run. How do I do that?
    > I want to do something like:
    > char *func_to_run = scanf(the, good, args);
    > pthread_create (thread, attr, (magic cast)func_to_run, arg);
    >
    > Even better, is it possible to test, at run time, that the provided
    > name is the name of a function that exists somewhere in the
    > compiled code?
    >
    > Thanks for any help


    The group has discussed your problem many times before, AFAICT, the
    consensus is that this cannot be done within Standard C code. Or
    maybe it *can* be done, but then you might have to write an awful
    lot of code to accomplish it.

    Someone much more experienced than me will probably explain shortly.
     
    santosh, Jul 18, 2007
    #2
    1. Advertising

  3. etienne <> wrote:
    > I am looking for help for a cast problem:
    > I want to run functions in thread, using the pthread_create function.
    > The function to run is the first argument of pthread_create and is
    > expected to be of type void* (*func)(void*)
    > My problem is that I want to get the name of the function to run in
    > a .txt file that I parse at runtime.
    > I read the file and I get the name in a char* variable.
    > Now I want that this char* variable is the name of the function to
    > run. How do I do that?
    > I want to do something like:
    > char *func_to_run = scanf(the, good, args);
    > pthread_create (thread, attr, (magic cast)func_to_run, arg);


    Unfortunately, there's no "magic cast" that can convert a
    string into a pointer to a function of that name, simply
    because information about function and variable names is
    not stored in the program - functions and variables get
    converted to addresses during compilation (or at least
    linking).

    > Even better, is it possible to test, at run time, that the provided
    > name is the name of a function that exists somewhere in the compiled
    > code?


    If you don't insist on a "magic cast" there's a trival solution.
    Just create an array of structures of a type like this:

    struct name2address {
    const char *name;
    void * ( * addr ) ( void * );
    };

    Make the array large enough to hold as many structures as you
    have functions that you want to know by both name and address
    and then initialize the array accordingly. Now, when you read
    in your file you just have to search this array for the element
    where the name field is identical to the name of the function
    you read from the text file. This then gives you the address of
    function which you then can use to call it. And a failure to
    find the function in the array of structures immediately tells
    you that what you did read from the text file was invalid.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jul 18, 2007
    #3
  4. etienne

    SM Ryan Guest

    (Jens Thoms Toerring) wrote:
    # etienne <> wrote:
    # > I am looking for help for a cast problem:
    # > I want to run functions in thread, using the pthread_create function.
    # > The function to run is the first argument of pthread_create and is
    # > expected to be of type void* (*func)(void*)
    # > My problem is that I want to get the name of the function to run in
    # > a .txt file that I parse at runtime.
    # > I read the file and I get the name in a char* variable.
    # > Now I want that this char* variable is the name of the function to
    # > run. How do I do that?
    # > I want to do something like:
    # > char *func_to_run = scanf(the, good, args);
    # > pthread_create (thread, attr, (magic cast)func_to_run, arg);
    #
    # Unfortunately, there's no "magic cast" that can convert a
    # string into a pointer to a function of that name, simply
    # because information about function and variable names is

    There is no magic cast in ANSI C, but there may be system
    specific ways to get this. For example, you may be able to
    get a load map from the loader and postprocess this into
    a function name map; but this would specific to your loader
    and development system.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    If you plan to shoplift, let us know.
    Thanks
     
    SM Ryan, Jul 18, 2007
    #4
  5. etienne

    Eric Sosman Guest

    etienne wrote:
    > Hello,
    > I am looking for help for a cast problem:
    > I want to run functions in thread, using the pthread_create function.
    > The function to run is the first argument of pthread_create and is
    > expected to be of type void* (*func)(void*)
    > My problem is that I want to get the name of the function to run in
    > a .txt file that I parse at runtime.
    > I read the file and I get the name in a char* variable.
    > Now I want that this char* variable is the name of the function to
    > run. How do I do that?
    > I want to do something like:
    > char *func_to_run = scanf(the, good, args);
    > pthread_create (thread, attr, (magic cast)func_to_run, arg);
    >
    > Even better, is it possible to test, at run time, that the provided
    > name is the name of a function that exists somewhere in the compiled
    > code?


    This is Question 20.6 in the comp.lang.c Frequently
    Asked Questions (FAQ) list, <http://c-faq.com/>.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 18, 2007
    #5
  6. etienne

    regis Guest

    etienne wrote:

    > I want to do something like:
    > char *func_to_run = scanf(the, good, args);
    > pthread_create (thread, attr, (magic cast)func_to_run, arg);
    >
    > Even better, is it possible to test, at run time, that the provided
    > name is the name of a function that exists somewhere in the compiled
    > code?


    System specific solutions exist to call the dynamic linking loader:
    You might want to look at the man page of functions
    dlopen(), dlsym(), dlclose()...

    --
    regis
     
    regis, Jul 18, 2007
    #6
  7. etienne

    etienne Guest

    On 18 juil, 15:46, regis <> wrote:
    > etienne wrote:
    > > I want to do something like:
    > > char *func_to_run = scanf(the, good, args);
    > > pthread_create (thread, attr, (magic cast)func_to_run, arg);

    >
    > > Even better, is it possible to test, at run time, that the provided
    > > name is the name of a function that exists somewhere in the compiled
    > > code?

    >
    > System specific solutions exist to call the dynamic linking loader:
    > You might want to look at the man page of functions
    > dlopen(), dlsym(), dlclose()...
    >
    > --
    > regis



    These functions do more or less what I want to do. I will try to use
    them.
    Thanks for your help.
     
    etienne, Jul 19, 2007
    #7
    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:
    685
    Ben Bacarisse
    May 23, 2008
  4. Replies:
    1
    Views:
    349
  5. Hansen
    Replies:
    3
    Views:
    1,154
    rep_movsd
    Apr 24, 2010
Loading...

Share This Page