C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • String of function names

    2 answers - 353 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    If somehow a bunch of function names in string can be passed into an
    executible, is it possible that for each name call, it will trigger the
    corresponding function of that name? course, this can be done by mapping
    string function names with functions themselves? Is there a better and
    direct way?
    Thanks in advance!
  • No.1 | | 630 bytes | |

    newhand wrote:

    If somehow a bunch of function names in string can be passed into an
    executible, is it possible that for each name call,
    it will trigger the
    corresponding function of that name?
    course, this can be done by mapping
    string function names with functions themselves?

    I use arrays of structures for that purpose.

    struct sf {
    void(*sortfunc)(e_type *, size_t);
    char *name;
    char *description;
    double total_time;

    };

    struct sf s_L[] = SRT_LIST;
    struct df {
    void(*distfunc)(e_type *, size_t, long unsigned *);
    char *string;
    } d_L[] = DISTRIBUTIN_LIST;
  • No.2 | | 1568 bytes | |

    newhand wrote:
    If somehow a bunch of function names in string can be passed into an
    executible, is it possible that for each name call, it will trigger the
    corresponding function of that name? course, this can be done by mapping
    string function names with functions themselves?

    If you're refering to searching through an array/list of strings of
    function name to find the corresponding function pointer, then yes
    that's the standard wrok-around.

    Is there a better and direct way?

    No.

    Actually, you can in theory get the addresses of your
    functions/subroutine (at assembly & machine code level functions are
    really just subroutines). But only if your executable is compiled with
    debugging symbols (-g option for gnu compilers). That's how debuggers
    work. Remember that at machine code level the function names don't
    exist. They are just addresses in memory and machines tend to have
    numbers as addresses ;-)

    Anyway, you really don't want this as this prevents you form generating
    optimised executales on most compilers.

    common design practice in embedded systems is to call functions by
    numbers. Protocols, commands and datatypes tend to be a one-byte field
    in packets. So you would use an array of 256 function pointers with
    each index in the array pointing to the appropriate packet handler for
    a given command. It's also very fast since indexing an array is much
    faster than both searching strings or if-else statements.

Re: String of function names


max 4000 letters.
Your nickname that display:
In order to stop the spam: 1 + 0 =
QUESTION ON "C/C++"

EMSDN.COM