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


Groups > comp.lang.c > #25881

Re: Do not cast pointers to functions to pointers to primitive types

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Do not cast pointers to functions to pointers to primitive types
Date 2012-09-03 18:19 -0700
Organization None to speak of
Message-ID <lnd322vb2p.fsf@nuthaus.mib.org> (permalink)
References <fce4988c-58d7-4847-84fe-edb1ed3b722c@googlegroups.com> <k227u5$i0a$1@dont-email.me> <lnzk56vov4.fsf@nuthaus.mib.org> <%N91s.204532$GZ3.83537@tornado.fastwebnet.it>

Show all headers | View raw


Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> writes:
> On 03/09/2012 22:21, Keith Thompson wrote:
> [..]
>> A concrete example:
>>
>>      int obj;
>>      void *vp = &obj;
>>      void (*fp)(void) = (void (*)(void))vp;
>>
>> The constraints on a cast operator are described in N1570 6.5.4.
>> The cast in the above code doesn't violate any of them.  (Perhaps
>> there*should*  be such a constraint.)
>>
>> Or am I missing something?
>
> My understanding is that there is not such a constraint to allow
> such casts as a "common extension", N1570 J.5.7:
>
>    A pointer to an object or to void may be cast to a pointer
>    to a function, allowing data to be invoked as a function (6.5.4).
>
>    A pointer to a function may be cast to a pointer to an object or to
>    void, allowing a function to be inspected or modified (for example,
>    by a debugger) (6.5.4).
>
> If that cast violated a constraint any implementation with such
> extension would be nonconforming. I only see a warning with gcc
> however when `-pedantic' is enabled.

It's not necessary to omit a constraint to allow an extension like
this.  Many extensions give meaning to code that would otherwise
violate a constraint or syntax rule.  The compiler has to issue
a diagnostic (which can be a non-fatal warning) if it's invoked
in conforming mode; once it's issued the warning, it's free to do
whatever the extension specifies.  And/or the compiler can have a
non-conforming mode that implements the extension without complaint.

An example: gcc permits arithmetic on void* pointers as an extension.
Any such arithmetic is a constraint violation.

Looking through the list of common extensions in J.5, some of
them can break strictly conforming programs, which violates 4p6.
Predefined macro names not starting with an underscore are one
example.

-- 
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.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Do not cast pointers to functions to pointers to primitive types ajaybgr <ajay.bgr@gmail.com> - 2012-09-03 01:50 -0700
  Re: Do not cast pointers to functions to pointers to primitive types Ian Collins <ian-news@hotmail.com> - 2012-09-03 20:59 +1200
  Re: Do not cast pointers to functions to pointers to primitive types Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-09-03 02:13 -0700
  Re: Do not cast pointers to functions to pointers to primitive types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 11:24 +0100
  Re: Do not cast pointers to functions to pointers to primitive types James Kuyper <jameskuyper@verizon.net> - 2012-09-03 08:31 -0400
    Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 13:21 -0700
      Re: Do not cast pointers to functions to pointers to primitive types James Kuyper <jameskuyper@verizon.net> - 2012-09-03 16:57 -0400
        Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 14:48 -0700
          Re: Do not cast pointers to functions to pointers to primitive types pacman@kosh.dhis.org (Alan Curry) - 2012-09-03 21:57 +0000
            Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 18:11 -0700
              Re: Do not cast pointers to functions to pointers to primitive types pacman@kosh.dhis.org (Alan Curry) - 2012-09-04 02:03 +0000
                Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 19:20 -0700
                Re: Do not cast pointers to functions to pointers to primitive types pacman@kosh.dhis.org (Alan Curry) - 2012-09-04 20:27 +0000
                Re: Do not cast pointers to functions to pointers to primitive types James Kuyper <jameskuyper@verizon.net> - 2012-09-04 17:10 -0400
                Re: Do not cast pointers to functions to pointers to primitive types Barry Schwarz <schwarzb@dqel.com> - 2012-09-04 14:14 -0700
          Re: Do not cast pointers to functions to pointers to primitive types pete <pfiland@mindspring.com> - 2012-09-03 22:51 -0400
      Re: Do not cast pointers to functions to pointers to primitive types Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2012-09-04 00:02 +0200
        Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 18:19 -0700
  Re: Do not cast pointers to functions to pointers to primitive types Kaz Kylheku <kaz@kylheku.com> - 2012-09-04 01:18 +0000
    Re: Do not cast pointers to functions to pointers to primitive types "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-09-04 11:59 -0400
      Re: Do not cast pointers to functions to pointers to primitive types Philip Lantz <prl@canterey.us> - 2012-09-06 21:34 -0700

csiph-web