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


Groups > comp.lang.c > #384223

Re: Casting the return value of ...

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Casting the return value of ...
Date 2024-04-08 23:41 -0700
Organization A noiseless patient Spider
Message-ID <86bk6j2g2c.fsf@linuxsc.com> (permalink)
References (3 earlier) <uu4k1c$3pq71$1@dont-email.me> <uu6dtk$a076$1@dont-email.me> <uu6fss$agvi$1@dont-email.me> <86plvchxpn.fsf@linuxsc.com> <uu8s6n$v2o8$2@dont-email.me>

Show all headers | View raw


bart <bc@freeuk.com> writes:

> On 30/03/2024 09:32, Tim Rentsch wrote:
>
>> bart <bc@freeuk.com> writes:
>>
>>> I was aware of the double conversion but KT used 'a cast' so I
>>> wondered if there was a single cast that could be used.
>>
>> There is not, if it's important that it work reliably across
>> different compilers and different platforms.
>>
>>> It is odd however that function and object pointers can be
>>> considered so different that even an explicit conversion
>>> between them is deemed to be meaningless.
>>
>> Function pointers and object pointers don't have to be the same
>> size, or use the same form of representation.  The C standard
>> allows implementations where code and data live in completely
>> separate memories.  In such cases there is no sensible way to
>> convert between the two kinds of pointers, because the two kinds
>> of addresses have no relationship to each other.
>
> Suppose a object pointer is 32 bits, and a function pointer is a
> 32-byte descriptor.
>
> An implementation could choose to present a function pointer as a
> 32-bit object pointer, which points to the full 32-byte descriptor in
> data memory.
>
> The simplest way of doing that is to have, for each function (or each
> one whose address is taken), a fixed corresponding descriptor in data
> memory.  So here function and object pointers can be exactly the same
> size, and can both refer to data memory, as far as the programmer is
> concerned.
>
> Dereferencing such a function pointer, to call the function, will
> involve an extra bit of indirection.  It would need something extra
> anyway to deal with those 32 bytes.

Two problems.  One, even if the proposed scheme is workable in some
cases that doesn't mean it will be in all cases.  Two, it imposes
what may be a significant cost but offers essentially no benefit.
The only useful thing that can be done with a converted function
pointer is cast it to an appropriate function pointer type so that
the function can be called.  If someone wants to have values and
variables that can hold both object pointers and function pointers
it is easy enough to do that by using a union:

    typedef union { void *pv; void (*pf)(); } VorF;

with no hidden implementation machinery needed.  There is no good
reason to gussy up the language or have implementations jump
through hoops when the needed capability is already present in
the language as it is now (and has been for more than 30 years).

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Casting the return value of ... gazelle@shell.xmission.com (Kenny McCormack) - 2024-03-28 15:09 +0000
  Re: Casting the return value of ... Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-28 18:16 +0000
    Re: Casting the return value of ... scott@slp53.sl.home (Scott Lurndal) - 2024-03-28 18:53 +0000
      Re: Casting the return value of ... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-30 10:33 -0700
        Re: Casting the return value of ... scott@slp53.sl.home (Scott Lurndal) - 2024-03-30 19:29 +0000
          Re: Casting the return value of ... Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-03-30 23:05 +0000
            Re: Casting the return value of ... scott@slp53.sl.home (Scott Lurndal) - 2024-03-30 23:25 +0000
            Re: Casting the return value of ... David Brown <david.brown@hesbynett.no> - 2024-03-31 15:44 +0200
            Re: Casting the return value of ... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-03-31 13:15 -0700
          Re: Casting the return value of ... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-08 23:01 -0700
            Re: Casting the return value of ... David Brown <david.brown@hesbynett.no> - 2024-04-09 10:03 +0200
    Re: Casting the return value of ... Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-28 12:38 -0700
      Re: Casting the return value of ... bart <bc@freeuk.com> - 2024-03-28 20:30 +0000
        Re: Casting the return value of ... Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-28 14:07 -0700
          Re: Casting the return value of ... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-03-28 14:15 -0700
          Re: Casting the return value of ... Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-28 21:44 +0000
            Re: Casting the return value of ... Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-28 22:01 +0000
              Re: Casting the return value of ... Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-28 22:33 +0000
                Re: Casting the return value of ... Michael S <already5chosen@yahoo.com> - 2024-03-29 15:53 +0200
                gcc Bugzilla search (was: Casting the return value of ...) Michael S <already5chosen@yahoo.com> - 2024-03-29 16:01 +0200
                Re: gcc Bugzilla search David Brown <david.brown@hesbynett.no> - 2024-03-29 17:00 +0100
            Re: Casting the return value of ... Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-28 15:37 -0700
          Re: Casting the return value of ... David Brown <david.brown@hesbynett.no> - 2024-03-29 14:06 +0100
            Re: Casting the return value of ... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-03-29 15:46 -0700
        Re: Casting the return value of ... David Brown <david.brown@hesbynett.no> - 2024-03-29 13:58 +0100
          Re: Casting the return value of ... bart <bc@freeuk.com> - 2024-03-29 13:32 +0000
            Re: Casting the return value of ... David Brown <david.brown@hesbynett.no> - 2024-03-29 17:10 +0100
            Re: Casting the return value of ... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-30 02:32 -0700
              Re: Casting the return value of ... bart <bc@freeuk.com> - 2024-03-30 11:14 +0000
                Re: Casting the return value of ... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-04-08 23:41 -0700
  Re: Casting the return value of ... Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-03-28 21:41 -0700
    Re: Casting the return value of ... Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-28 22:02 -0700
  Re: Casting the return value of ... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-29 15:52 -0700

csiph-web