Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1384

Re: Conversion from void* to pointer-to-function?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Re: Conversion from void* to pointer-to-function?
Date 2012-06-25 14:57 -0700
Organization None to speak of
Message-ID <lnhatz5a29.fsf@nuthaus.mib.org> (permalink)
References <ln62af6sum.fsf@nuthaus.mib.org> <4FE8D6E4.1060608@verizon.net>

Show all headers | View raw


James Kuyper <jameskuyper@verizon.net> writes:
> On 06/25/2012 04:26 PM, Keith Thompson wrote:
>> Consider the following toy program:
>> 
>> int main(void) {   
>>     void *void_ptr = 0;
>>     void (*func_ptr)(void) = (void(*)(void))void_ptr;
>>     return 0;
>> }
>> 
>> (Note that void_ptr is *not* a null pointer constant, it's merely a
>> pointer object that happens to contain a null pointer.)
>> 
>> Is the conversion of void_ptr to void(*)(void) (a pointer-to-function
>> type) permitted?
>> 
>> N1570 6.5.4 lists three constraints for the cast operator. This code
>> doesn't violate any of them.
>> 
>> N1570 6.3.2.3 describes the semantics of conversions involving pointers.
>> It mentions conversions from void* to and from pointer-to-object types,
>> conversions from one pointer-to-object type to another, and conversions
>> from one pointer-to-function type to another.  Conversions from void* to
>> a pointer-to-function type simply aren't mentioned.
>
> Conversion of null pointers to other pointer types is mentioned
> (6.3.6.3p4). It applies only to null pointers being converted to pointer
> types, but there are no other type-based restrictions on the
> applicability of that section.
>
>> My conclusion is that the conversion is permitted (it doesn't violate
>> any constraint or syntax rule), but its behavior is undefined by
>> omission.
>
> The behavior is defined by 6.3.6.3p4: it produces a null pointer of type
> void(*)(void).

It's 6.3.2.3p4, not 6.3.6.3p4.

You're right, of course -- which makes me realize I didn't ask what I
intended to ask.  I meant to ask about conversion from void* to a
pointer-to-function type in general, not about conversion of null
pointers.

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.

-- 
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


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