|
Home > Archive > Unix Programming > March 2006 > what type should I cast?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
what type should I cast?
|
|
|
| Hello all:
I'm using a pointer array(by gtk+ lib) to store the function
pointers (in line 83).
Now. I have a big problem. As I get the element of the pointer array, I
have no idea how to cast void pointer in to my function pointer!!!!
The follows inst is my function pointer.
void (*inst)(core_ptr c, GArray* garray);
66 void* initialize_loadshareobjects(){
67 void *handle;
68 char *error;
69
70 handle = dlopen (SHARE_LIB_PATH, RTLD_NOW);
71
72 if (!handle){
73 fprintf (stderr, "%s\n", dlerror());
74 exit(1);
75 }
76 dlerror();
77 return handle;
78 }
79
80 void load_instruction(void *handle, GPtrArray* insts, gchar*
function_nm){
81 void (*inst)(core_ptr c, GArray* garray);
82 //*(void **) (&inst) = dlsym(handle, function_nm);
83 g_ptr_array_add(insts, dlsym(handle, function_nm));
84 }
85
Thanks for your reading!!
| |
| Måns Rullgård 2006-03-13, 5:54 pm |
| "s88" <dave.tw@gmail.com> writes:
> Hello all:
> I'm using a pointer array(by gtk+ lib) to store the function
> pointers (in line 83).
> Now. I have a big problem. As I get the element of the pointer array, I
> have no idea how to cast void pointer in to my function pointer!!!!
You don't need to cast void pointers. A simple, unadorned assignment
will the job perfectly. In fact, it is the *proper* way to do it.
Casting a void * is wrong, unless you use C++ (but then C++ is often
considered wrong in itself).
--
Måns Rullgård
mru@inprovide.com
| |
| Fletcher Glenn 2006-03-13, 5:54 pm |
| s88 wrote:
> Hello all:
> I'm using a pointer array(by gtk+ lib) to store the function
> pointers (in line 83).
> Now. I have a big problem. As I get the element of the pointer array, I
> have no idea how to cast void pointer in to my function pointer!!!!
> The follows inst is my function pointer.
> void (*inst)(core_ptr c, GArray* garray);
>
>
> 66 void* initialize_loadshareobjects(){
> 67 void *handle;
> 68 char *error;
> 69
> 70 handle = dlopen (SHARE_LIB_PATH, RTLD_NOW);
> 71
> 72 if (!handle){
> 73 fprintf (stderr, "%s\n", dlerror());
> 74 exit(1);
> 75 }
> 76 dlerror();
> 77 return handle;
> 78 }
> 79
> 80 void load_instruction(void *handle, GPtrArray* insts, gchar*
> function_nm){
> 81 void (*inst)(core_ptr c, GArray* garray);
> 82 //*(void **) (&inst) = dlsym(handle, function_nm);
> 83 g_ptr_array_add(insts, dlsym(handle, function_nm));
> 84 }
> 85
>
> Thanks for your reading!!
>
So what's wrong with the following:
void (*xxx)(core_ptr, GArray*);
xxx = (void(*)(core_ptr, GArray*)) dlsym(handle, function_nm);
After all void(*)(core_ptr, GArray*) reads as: "an unnamed pointer to a
function returning void with an argument list of (core_ptr, GArray*)".
--
Fletcher Glenn
| |
| Måns Rullgård 2006-03-13, 5:54 pm |
| Fletcher Glenn <fletcher@removethisfoglight.com> writes:
> So what's wrong with the following:
>
> void (*xxx)(core_ptr, GArray*);
> xxx = (void(*)(core_ptr, GArray*)) dlsym(handle, function_nm);
dlsym() returns a void *, so there is no need for a cast. Needlessly
casting things can hide serious problems that would otherwise have
triggered a compiler warning.
--
Måns Rullgård
mru@inprovide.com
| |
| Maxim Yegorushkin 2006-03-13, 5:54 pm |
| s88 wrote:
> Hello all:
> I'm using a pointer array(by gtk+ lib) to store the function
> pointers (in line 83).
> Now. I have a big problem. As I get the element of the pointer array, I
> have no idea how to cast void pointer in to my function pointer!!!!
> The follows inst is my function pointer.
> void (*inst)(core_ptr c, GArray* garray);
An illuminating discussion:
http://groups.google.com/group/comp...375c1a6ec2566c1
| |
| Fletcher Glenn 2006-03-14, 5:55 pm |
| Måns Rullgård wrote:
> Fletcher Glenn <fletcher@removethisfoglight.com> writes:
>
>
>
>
> dlsym() returns a void *, so there is no need for a cast. Needlessly
> casting things can hide serious problems that would otherwise have
> triggered a compiler warning.
>
I guess that I have really gotten used to C++ where not casting
a void * is impossible.
--
Fletcher Glenn
| |
| Micah Cowan 2006-03-14, 5:55 pm |
| Måns Rullgård <mru@inprovide.com> writes:
> Fletcher Glenn <fletcher@removethisfoglight.com> writes:
>
>
> dlsym() returns a void *, so there is no need for a cast. Needlessly
> casting things can hide serious problems that would otherwise have
> triggered a compiler warning.
Generally, this is very good advice. However, in this case it's
wrong. void* can only be implicitly converted to pointers to
incomplete or object type. In this case, the type is a pointer to
function, and requires a diagnostic.
gcc won't complain, though, unless you give it the -pedantic flag.
For maximum portability, though, the cast is a good idea.
Note that C doesn't guarantee that casting it is even well-defined;
but SUS implies it through its definition of dlsym(). It also
specifically calls out this problem in its Rationale (which seems to
have a bug or two).
| |
| Jordan Abel 2006-03-14, 5:55 pm |
| On 2006-03-13, Måns Rullgård <mru@inprovide.com> wrote:
> Fletcher Glenn <fletcher@removethisfoglight.com> writes:
>
>
> dlsym() returns a void *, so there is no need for a cast. Needlessly
> casting things can hide serious problems that would otherwise have
> triggered a compiler warning.
Well, yes, except... well, it's been explained by others in this thread.
This just goes to show that you shouldn't just automatically post
boilerplate answers/criticism like this without a reason. dlsym is not
malloc, its return value is more likely than not to require a cast,
since it's most often used, I suspect, for functions.
| |
| Måns Rullgård 2006-03-14, 5:55 pm |
| Jordan Abel <random832@gmail.com> writes:
> On 2006-03-13, Måns Rullgård <mru@inprovide.com> wrote:
>
> Well, yes, except... well, it's been explained by others in this thread.
> This just goes to show that you shouldn't just automatically post
> boilerplate answers/criticism like this without a reason. dlsym is not
> malloc, its return value is more likely than not to require a cast,
> since it's most often used, I suspect, for functions.
I stand corrected.
--
Måns Rullgård
mru@inprovide.com
|
|
|
|
|