Deutsch English Français Italiano |
<uu6edi$a076$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Casting the return value of ... Date: Fri, 29 Mar 2024 14:06:58 +0100 Organization: A noiseless patient Spider Lines: 58 Message-ID: <uu6edi$a076$2@dont-email.me> References: <uu416t$33u55$1@news.xmission.com> <20240328105203.773@kylheku.com> <87frwatadu.fsf@nosuchdomain.example.com> <uu4k1c$3pq71$1@dont-email.me> <87bk6yt68v.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 29 Mar 2024 13:06:59 +0100 (CET) Injection-Info: dont-email.me; posting-host="528a481da3bf32c95b1c8190e81e5c9b"; logging-data="327910"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WRfgdBIR8U9gYQmV8GeLg6qXovZaPvMs=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:tgd50AQzHt+YiHfVox3LVgbhD3U= In-Reply-To: <87bk6yt68v.fsf@nosuchdomain.example.com> Content-Language: en-GB Bytes: 3463 On 28/03/2024 22:07, Keith Thompson wrote: > bart <bc@freeuk.com> writes: >> On 28/03/2024 19:38, Keith Thompson wrote: >>> Kaz Kylheku <433-929-6894@kylheku.com> writes: >>> [...] >>>> Conversions between function pointers and data pointers are an >>>> extension; it is not well-defined behavior in ISO C. >>>> >>>> Therefore we can neither say that ISO C doesn't require a cast there (it >>>> imposes no requirements at all), nor that the conversion is fine with a >>>> cast. >>>> >>>> The cast is /likely/ necessary, in order to correctly trigger the >>>> extension. >>> ISO C does require a cast. The cast is necessary to avoid a >>> constraint violation and a mandatory diagnostic. The resulting >>> behavior is undefined in ISO C, but defined by POSIX. Assigning a >>> void* value to a pointer-to-function object without a cast violates >>> the constraint for simple assignment (N1570 6.5.16.1p1). >> >> What would such a cast look like? Since this gives a warning with >> -Wpedantic even with a cast: >> >> void* p; >> void(*q)(void); >> >> p=(void*)q; >> q=(void(*)(void))p; > > The warnings I get from gcc are: > > warning: ISO C forbids conversion of function pointer to object pointer type [-Wpedantic] > warning: ISO C forbids conversion of object pointer to function pointer type [-Wpedantic] > > With -pedantic-errors, these become fatal errors. > > I disagree with gcc. ISO C doesn't define the behavior, but it doesn't > forbid the conversion. (Anyone who disagrees is invited to cite the > constraint that it violates.) > I think that the C standards don't forbid the conversion, but the description of pointer conversions (6.3.2.3) does not describe such conversions. That makes it, AFAICS, undefined behaviour rather than "forbidden" (which I would define as something that mandates a diagnostic). Dereferencing such converted pointers might be undefined behaviour (if you haven't converted back to the original type), or implementation-dependent behaviour (if the conversions change the bitwise representation of the pointer). I personally think it's good that gcc has this diagnostic, even if the message text is not strictly accurate. > Note that clang doesn't issue this diagnostic. >