472,099 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,099 software developers and data experts.

cast to pointer to function

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

Jul 18 '07 #1
6 2673
On Wednesday 18 Jul 2007 3:25 pm, in comp.lang.c, etienne
<et***********@lip6.frwrote:
Message ID: <11**********************@g12g2000prg.googlegroups .com>
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.
Jul 18 '07 #2
etienne <et***********@lip6.frwrote:
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 ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 18 '07 #3
jt@toerring.de (Jens Thoms Toerring) wrote:
# etienne <et***********@lip6.frwrote:
# 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
Jul 18 '07 #4
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
es*****@ieee-dot-org.invalid
Jul 18 '07 #5
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
Jul 18 '07 #6
On 18 juil, 15:46, regis <regis.barbanc...@free.frwrote:
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.

Jul 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Graham Nicholls | last post: by
5 posts views Thread by Patrick Guio | last post: by
10 posts views Thread by LaEisem | last post: by
9 posts views Thread by Frederick Gotham | last post: by
7 posts views Thread by llothar | last post: by
18 posts views Thread by Felix Kater | last post: by
1 post views Thread by etienne | last post: by
5 posts views Thread by =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post: by
3 posts views Thread by Bartc | last post: by
7 posts views Thread by * Tong * | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.