Re: Casting the return value of ...

Liste des GroupesRevenir à l c 
Sujet : Re: Casting the return value of ...
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 29. Mar 2024, 15:06:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uu6edi$a076$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
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.
 

Date Sujet#  Auteur
28 Mar 24 * Casting the return value of ...31Kenny McCormack
28 Mar 24 +* Re: Casting the return value of ...27Kaz Kylheku
28 Mar 24 i+* Re: Casting the return value of ...20Keith Thompson
28 Mar 24 ii`* Re: Casting the return value of ...19bart
28 Mar 24 ii +* Re: Casting the return value of ...12Keith Thompson
28 Mar 24 ii i+- Re: Casting the return value of ...1Chris M. Thomasson
28 Mar 24 ii i+* Re: Casting the return value of ...8Kaz Kylheku
29 Mar 24 ii ii+* Re: Casting the return value of ...5Kaz Kylheku
29 Mar 24 ii iii`* Re: Casting the return value of ...4Kaz Kylheku
29 Mar 24 ii iii `* Re: Casting the return value of ...3Michael S
29 Mar 24 ii iii  `* gcc Bugzilla search (was: Casting the return value of ...)2Michael S
29 Mar 24 ii iii   `- Re: gcc Bugzilla search1David Brown
29 Mar 24 ii ii+- Re: Casting the return value of ...1Keith Thompson
8 Jun 24 ii ii`- Re: Casting the return value of ...1Tim Rentsch
29 Mar 24 ii i`* Re: Casting the return value of ...2David Brown
30 Mar 24 ii i `- Re: Casting the return value of ...1Chris M. Thomasson
29 Mar 24 ii `* Re: Casting the return value of ...6David Brown
29 Mar 24 ii  `* Re: Casting the return value of ...5bart
29 Mar 24 ii   +- Re: Casting the return value of ...1David Brown
30 Mar 24 ii   `* Re: Casting the return value of ...3Tim Rentsch
30 Mar 24 ii    `* Re: Casting the return value of ...2bart
9 Apr 24 ii     `- Re: Casting the return value of ...1Tim Rentsch
30 Mar 24 i`* Re: Casting the return value of ...6Tim Rentsch
31 Mar 24 i +* Re: Casting the return value of ...3Lawrence D'Oliveiro
31 Mar 24 i i+- Re: Casting the return value of ...1David Brown
31 Mar 24 i i`- Re: Casting the return value of ...1Chris M. Thomasson
9 Apr 24 i `* Re: Casting the return value of ...2Tim Rentsch
9 Apr 24 i  `- Re: Casting the return value of ...1David Brown
29 Mar 24 +* Re: Casting the return value of ...2Andrey Tarasevich
29 Mar 24 i`- Re: Casting the return value of ...1Keith Thompson
30 Mar 24 `- Re: Casting the return value of ...1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal