Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - function * = void *

 
Thread Tools Search this Thread
Old 06-21-2006, 07:07 PM   #1
Default function * = void *


The following code generates a compiler warning
when compiled with gcc -pedantic:

typedef (*FUNC)(int);
FUNC f;

void *
get_f(void)
{
return &f;
}

int main(void)
{
FUNC fp;
fp = (FUNC)get_f();
return 0;
}
~

The warning is:
a.c: In function `main':
a.c:14: warning: ISO C forbids conversion of object pointer to function
pointer type

If I remove the cast, the warning is:
a.c: In function `main':
a.c:14: warning: ISO C forbids assignment between function pointer and
`void *'


Surely I can use this construction? Can I safely ignore the warning?
I'd prefer to keep -pedantic in my CFLAGS---how can I
correctly suppress the warning?

Or am I doing something wrong?



Bill Pursell
  Reply With Quote
Old 06-21-2006, 07:13 PM   #2
Ben Pfaff
 
Posts: n/a
Default Re: function * = void *
"Bill Pursell" <> writes:

> typedef (*FUNC)(int);
> FUNC f;
>
> void *
> get_f(void)
> {
> return &f;
> }


[...]

> Or am I doing something wrong?


Yes. The standard doesn't allow conversion between function and
object pointers. Why not change get_f() to return a function
pointer? It doesn't have to be the *correct* kind of function
pointer: casts from one kind of function pointer to another are
allowed and well-defined, as long as you don't actually call a
function through an incompatible function pointer.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Ben Pfaff
  Reply With Quote
Old 06-21-2006, 07:22 PM   #3
Gordon Burditt
 
Posts: n/a
Default Re: function * = void *
>The following code generates a compiler warning
>when compiled with gcc -pedantic:
>
>typedef (*FUNC)(int);
>FUNC f;
>
>void *
>get_f(void)
>{
> return &f;
>}
>
>int main(void)
>{
> FUNC fp;
> fp = (FUNC)get_f();
> return 0;
>}
>~
>
>The warning is:
>a.c: In function `main':
>a.c:14: warning: ISO C forbids conversion of object pointer to function
>pointer type
>
>If I remove the cast, the warning is:
>a.c: In function `main':
>a.c:14: warning: ISO C forbids assignment between function pointer and
>`void *'
>
>
>Surely I can use this construction?


There is no guarantee that a function pointer can FIT in a void *
without truncating it. On Intel x86 platforms, there are medium
and compact models where the size of a function pointer is not the
same as that of a data pointer (on one the data pointer is larger,
on the other the function pointer is larger).

It wouldn't surprise me if there will be new platforms where there
are function and data pointers of 64-bits (or perhaps 48 bits) and
32-bits. The issue isn't just ancient hardware.

You *can* use the construct fflush(main) if you can find even one
platform it works on. I don't know of any country where you would
be arrested for that. I don't understand why this is an issue.

>Can I safely ignore the warning?


It depends on how portable you want your code to be, and what
platform you are actually running on. You are writing code
which makes invalid assumptions and the problem isn't likely
to just be ancient hardware.

>I'd prefer to keep -pedantic in my CFLAGS---how can I
>correctly suppress the warning?
>
>Or am I doing something wrong?


According to ANSI C, you are doing something wrong.

Gordon L. Burditt


Gordon Burditt
  Reply With Quote
Old 06-21-2006, 07:58 PM   #4
Bill Pursell
 
Posts: n/a
Default Re: function * = void *

Ben Pfaff wrote:
> "Bill Pursell" <> writes:
>
> > typedef (*FUNC)(int);
> > FUNC f;
> >
> > void *
> > get_f(void)
> > {
> > return &f;
> > }

>
> [...]
>
> > Or am I doing something wrong?

>
> Yes. The standard doesn't allow conversion between function and
> object pointers. Why not change get_f() to return a function
> pointer? It doesn't have to be the *correct* kind of function
> pointer: casts from one kind of function pointer to another are
> allowed and well-defined, as long as you don't actually call a
> function through an incompatible function pointer.


This may be OT, as it's system specific, but how am I supposed
to use dlopen()? It's prototyped to return a void *. Should I do
something like: (doesn't compile)

typedef int (*FUNC)(int);
FUNC f;

typedef FUNC *(F_GETTER)(void);

void * dlopen(void) { return &f; }

int main(void)
{
FUNC fp;
fp = ((F_GETTER)dlopen)();
}



Bill Pursell
  Reply With Quote
Old 06-21-2006, 08:01 PM   #5
Bill Pursell
 
Posts: n/a
Default Re: function * = void *

Bill Pursell wrote:
> Ben Pfaff wrote:
> > "Bill Pursell" <> writes:
> >
> > > typedef (*FUNC)(int);
> > > FUNC f;
> > >
> > > void *
> > > get_f(void)
> > > {
> > > return &f;
> > > }

> >
> > [...]
> >
> > > Or am I doing something wrong?

> >
> > Yes. The standard doesn't allow conversion between function and
> > object pointers. Why not change get_f() to return a function
> > pointer? It doesn't have to be the *correct* kind of function
> > pointer: casts from one kind of function pointer to another are
> > allowed and well-defined, as long as you don't actually call a
> > function through an incompatible function pointer.

>
> This may be OT, as it's system specific, but how am I supposed
> to use dlopen()? It's prototyped to return a void *. Should I do
> something like: (doesn't compile)
>
> typedef int (*FUNC)(int);
> FUNC f;
>
> typedef FUNC *(F_GETTER)(void);
>
> void * dlopen(void) { return &f; }
>
> int main(void)
> {
> FUNC fp;
> fp = ((F_GETTER)dlopen)();
> }


oops, I meant dlsym() in the above.



Bill Pursell
  Reply With Quote
Old 06-21-2006, 08:27 PM   #6
William Ahern
 
Posts: n/a
Default Re: function * = void *
On Wed, 21 Jun 2006 12:01:08 -0700, Bill Pursell wrote:
> Bill Pursell wrote:
>> Ben Pfaff wrote:
>> > Yes. The standard doesn't allow conversion between function and
>> > object pointers. Why not change get_f() to return a function pointer?
>> > It doesn't have to be the *correct* kind of function pointer: casts
>> > from one kind of function pointer to another are allowed and
>> > well-defined, as long as you don't actually call a function through an
>> > incompatible function pointer.

>>
>> This may be OT, as it's system specific, but how am I supposed to use
>> dlopen()? It's prototyped to return a void *. Should I do something
>> like: (doesn't compile)
>>

<snip>
> oops, I meant dlsym() in the above.


dlopen() and dlsym() aren't standard C. They're Unix specific. It's a fair
bet that if they're available on your platform, then explicitly casting
between function pointers and void pointers will work as desired. But such
C code, as well as the use of those interfaces, isn't portable outside of
Unix. Here's what SUSv3 has to say about the issue:

RATIONALE

The ISO C standard does not require that pointers to functions can be
cast back and forth to pointers to data. Indeed, the ISO C standard does
not require that an object of type void * can hold a pointer to a
function. Implementations supporting the XSI extension, however, do
require that an object of type void * can hold a pointer to a function.
The result of converting a pointer to a function into a pointer to
another data type (except void *) is still undefined, however. Note that
compilers conforming to the ISO C standard are required to generate a
warning if a conversion from a void * pointer to a function pointer is
attempted as in:

fptr = (int (*)(int))dlsym(handle, "my_function");

Due to the problem noted here, a future version may either add a new
function to return function pointers, or the current interface may be
deprecated in favor of two new functions: one that returns data pointers
and the other that returns function pointers.

Source: http://www.opengroup.org/onlinepubs/...ons/dlsym.html



William Ahern
  Reply With Quote
Old 06-21-2006, 10:12 PM   #7
Keith Thompson
 
Posts: n/a
Default Re: function * = void *
"Bill Pursell" <> writes:
> Ben Pfaff wrote:
>> "Bill Pursell" <> writes:
>>
>> > typedef (*FUNC)(int);
>> > FUNC f;
>> >
>> > void *
>> > get_f(void)
>> > {
>> > return &f;
>> > }

>>
>> [...]
>>
>> > Or am I doing something wrong?

>>
>> Yes. The standard doesn't allow conversion between function and
>> object pointers. Why not change get_f() to return a function
>> pointer? It doesn't have to be the *correct* kind of function
>> pointer: casts from one kind of function pointer to another are
>> allowed and well-defined, as long as you don't actually call a
>> function through an incompatible function pointer.

>
> This may be OT, as it's system specific, but how am I supposed
> to use dlopen()? It's prototyped to return a void *.


<OT>
According to the man page, dlopen() returns an opaque "handle" for the
dynamic library, not something to be used as a function pointer. It's
dlsym() that's the problem.
</OT>

An implementation may support conversion from void* to
pointer-to-function as an extension. If you have a function that
returns a void* and expects you to use it as a pointer-to-function,
that function would only be usable with an implementation that
supports such an extension. Any code that depends on such an
extension is obviously non-portable; if you invoke the compiler with
an option that asks it to warn you about non-portable code, it's going
to warn you about this.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson
  Reply With Quote
Old 06-22-2006, 07:38 AM   #8
jaysome
 
Posts: n/a
Default Re: function * = void *
On Wed, 21 Jun 2006 18:22:35 -0000, (Gordon
Burditt) wrote:

>>The following code generates a compiler warning
>>when compiled with gcc -pedantic:
>>
>>typedef (*FUNC)(int);
>>FUNC f;
>>
>>void *
>>get_f(void)
>>{
>> return &f;
>>}
>>
>>int main(void)
>>{
>> FUNC fp;
>> fp = (FUNC)get_f();
>> return 0;
>>}
>>~
>>
>>The warning is:
>>a.c: In function `main':
>>a.c:14: warning: ISO C forbids conversion of object pointer to function
>>pointer type
>>
>>If I remove the cast, the warning is:
>>a.c: In function `main':
>>a.c:14: warning: ISO C forbids assignment between function pointer and
>>`void *'
>>
>>
>>Surely I can use this construction?

>
>There is no guarantee that a function pointer can FIT in a void *
>without truncating it.


If that's the case, how should I printf() the value of a function
pointer?

#include <stdio.h>
typedef void (*func_ptr_type)(void);
int main(void)
{
func_ptr_type func_ptr = NULL;
printf("val = %p\n", (void *)func_ptr);
return 0;
}

The "%p" conversion specifier requires a (void*), but if I convert a
function pointer to (void*) and that gets "truncated", isn't there a
serious flaw in the standard?

--
jay


jaysome
  Reply With Quote
Old 06-22-2006, 08:19 AM   #9
Keith Thompson
 
Posts: n/a
Default Re: function * = void *
jaysome <> writes:
> On Wed, 21 Jun 2006 18:22:35 -0000, (Gordon
> Burditt) wrote:

[...]
>>There is no guarantee that a function pointer can FIT in a void *
>>without truncating it.

>
> If that's the case, how should I printf() the value of a function
> pointer?
>
> #include <stdio.h>
> typedef void (*func_ptr_type)(void);
> int main(void)
> {
> func_ptr_type func_ptr = NULL;
> printf("val = %p\n", (void *)func_ptr);
> return 0;
> }
>
> The "%p" conversion specifier requires a (void*), but if I convert a
> function pointer to (void*) and that gets "truncated", isn't there a
> serious flaw in the standard?


There's no portable way to print the value of a function pointer,
other than by aliasing it to an array of unsigned char and printing
the bytes.

I don't think it's a serious flaw; how often do you need to print the
value of a function pointer?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson
  Reply With Quote
Old 06-22-2006, 08:42 AM   #10
Simon Biber
 
Posts: n/a
Default Re: function * = void *
jaysome wrote:
> On Wed, 21 Jun 2006 18:22:35 -0000, (Gordon
> Burditt) wrote:
>> There is no guarantee that a function pointer can FIT in a void *
>> without truncating it.

>
> If that's the case, how should I printf() the value of a function
> pointer?


Byte by byte.

> #include <stdio.h>
> typedef void (*func_ptr_type)(void);
> int main(void)
> {
> func_ptr_type func_ptr = NULL;
> printf("val = %p\n", (void *)func_ptr);
> return 0;
> }


Try this:

#include <stdio.h>
#include <limits.h>
typedef int (*func_ptr_type)(void);
int main(void)
{
func_ptr_type func_ptr = main;
size_t i;
printf("val = 0x");
for(i = 0; i < sizeof func_ptr; i++)
{
printf("%0*X",
(CHAR_BIT + 3) / 4,
(unsigned)((unsigned char *)&func_ptr)[i]);
}
printf("\n");
return 0;
}

This compiles cleanly and prints val = 0x50104000
on my system.

> The "%p" conversion specifier requires a (void*), but if I convert a
> function pointer to (void*) and that gets "truncated", isn't there a
> serious flaw in the standard?


Not really. The %p specifier is only for printing void* pointers. It so
happens that all object pointers can be converted to unique void* values.

--
Simon.


Simon Biber
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent function for itoa in Linux gcc compiler suse Software 0 03-06-2009 05:30 AM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
How to call C# function in javascript visj4u Software 2 04-23-2007 03:24 PM
I lost the "Help and Support" function from my start menu Keith A+ Certification 1 03-14-2005 03:05 PM
TheDigitalReview: TOUCHING THE VOID - DVD REVIEW Mike McGee DVD Video 0 06-09-2004 08:47 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46