Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Re: Conversion from void* to pointer-to-function? |
| Date | 2012-06-27 11:33 -0700 |
| Organization | None to speak of |
| Message-ID | <ln7gus4nbg.fsf@nuthaus.mib.org> (permalink) |
| References | <ln62af6sum.fsf@nuthaus.mib.org> <4FE8D6E4.1060608@verizon.net> <lnhatz5a29.fsf@nuthaus.mib.org> <a5f80e86-c634-4c19-af6d-e1023cf63dae@googlegroups.com> |
Maxim Fomin <maxim@maxim-fomin.ru> writes:
[...]
>> A revised test case:
>>
>> #include <stdlib.h>
>> #include <assert.h>
>> int main(void) {
>> void *void_ptr = malloc(1024);
>> assert(void_ptr != NULL);
>> void (*func_ptr)(void) = (void(*)(void))void_ptr;
>> return 0;
>> }
>>
>> Assume that the assert doesn't fire.
>>
>> I believe that the conversion in the initializer for void_ptr is
>> legal (i.e., doesn't violate any syntax rule or constraint) but has
>> undefined behavior by omission. (The authors of gcc, based on the
>> warning given with "gcc -std=c99 -pedantic", apparently disagree.)
>>
>> Is my interpretation correct?
>>
>> I know that the standard explicitly allows for undefined behavior when
>> the behavior is quietly not specified, but IMHO the standard would be
>> improved if 6.3.2.3 covered this case and explicitly stated that the
>> behavior is undefined.
>
> I think there is another interpretation of this issue.
>
> 6.2.5 p.20: "A pointer type may be derived from a function type or an
> object type, called the referenced type". "referenced type" is
> highlighted and supposed to be important.
"referenced type" is in italics, which simply means that this is the
definition of the term (see 3p1).
> 6.3.2.1 p.8: "If a converted pointer is used to call a function whose
> type is not compatible with the referenced type, the behavior is
> undefined."
That's not relevant to this case. It refers to calling a function with a
function pointer of the wrong type. The prefix of a function call is an
expression of pointer-to-function type; see 6.5.2.2p1. If the prefix is
a pointer to an object type, that's a constraint violation.
> So, this statement should be applied to both object and function
> pointers and not only to function pointers. 6.2.5 states that there
> are two groups of pointers: pointers-to-object and pointers-to
> function. It doesn't state that there is a third case of void*
> pointer, so void* should be pointer-to-object because its type is
> incomplete (object types may be incomplete) and it may be freely
> converted to any pointer to object.
In C99, types are partitioned into object types, function types, and
incomplete types (void is an incompletetype).
C11 changes this; now types are partitioned into function typed and
object types. An object type may be incomplete or complete. (6.2.5p1).
Certainly void* may be converted to any pointer to object. My question
is about converting void* to a pointer to function.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-06-25 13:26 -0700
Re: Conversion from void* to pointer-to-function? James Kuyper <jameskuyper@verizon.net> - 2012-06-25 17:23 -0400
Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-06-25 14:57 -0700
Re: Conversion from void* to pointer-to-function? Maxim Fomin <maxim@maxim-fomin.ru> - 2012-06-27 10:03 -0700
Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-06-27 11:33 -0700
Re: Conversion from void* to pointer-to-function? David R Tribble <david@tribble.com> - 2012-07-24 20:05 -0700
Re: Conversion from void* to pointer-to-function? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-21 00:32 -0800
Re: Conversion from void* to pointer-to-function? Tijl Coosemans <tijl@coosemans.org> - 2012-12-21 10:00 +0100
Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-12-21 02:01 -0800
Re: Conversion from void* to pointer-to-function? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-21 08:12 -0800
Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-12-21 17:58 -0800
Re: Conversion from void* to pointer-to-function? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-22 08:22 -0800
csiph-web