Deutsch English Français Italiano |
<86bk6j2g2c.fsf@linuxsc.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch <tr.17687@z991.linuxsc.com> Newsgroups: comp.lang.c Subject: Re: Casting the return value of ... Date: Mon, 08 Apr 2024 23:41:47 -0700 Organization: A noiseless patient Spider Lines: 55 Message-ID: <86bk6j2g2c.fsf@linuxsc.com> References: <uu416t$33u55$1@news.xmission.com> <20240328105203.773@kylheku.com> <87frwatadu.fsf@nosuchdomain.example.com> <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Tue, 09 Apr 2024 06:41:49 +0200 (CEST) Injection-Info: dont-email.me; posting-host="47558c70f7e061adbec47d824d7ff660"; logging-data="88023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194CHAZSSRHGz+n3C9478kPw9PxNVPiAaU=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:zIGnplwEL06x1AUWxRXpuOV6ulM= sha1:tU7U5QzcVCBpSzORID9ZZUKn+EM= Bytes: 3711 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).