 |
|
 |
|
03-13-06 10:54 PM
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!!
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-13-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-13-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-13-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-13-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-14-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-14-06 10: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).
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-14-06 10: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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: what type should I cast? |
 |
 |
|
|
03-14-06 10: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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 03:22 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|