String of function names

N

newhand

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? Of course, this can be done by mapping
string function names with functions themselves? Is there a better and
direct way?

Thanks in advance!
 
P

pete

newhand said:
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?
Of course, this can be done by mapping
string function names with functions themselves?

I use arrays of structures for that purpose.

http://groups.google.com/group/comp.programming/msg/e3e8e4c1766401d1

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

};



struct sf s_L[] = SORT_LIST;
struct df {
void(*distfunc)(e_type *, size_t, long unsigned *);
char *string;
} d_L[] = DISTRIBUTION_LIST;
 
S

slebetman

newhand said:
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? Of 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.

One 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.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top