Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: No, C is not a simple language
Date: Tue, 11 May 2021 05:18:29 -0700
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <864kf9h3ai.fsf@linuxsc.com>
References: <878s5f1im9.fsf@nosuchdomain.example.com> <86h7k2h66j.fsf@linuxsc.com> <87tuo2yqiq.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="ce98507e466523ba1e4f65ba30f9b784"; logging-data="9112"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19xHMocQeV49a4CLqq36dIaKAiZj2KX9qQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:bTWI8/qg3ULGcwemI99IYz4k8C4= sha1:kWNibBykfOQppW4cmzV8VzNSCyU=
Xref: csiph.com comp.lang.c:160729
Richard Damon writes:
> On 4/19/21 4:21 PM, Keith Thompson wrote:
>
>> Thiago Adams writes:
>>
>>> Last week I receive a warning in clang complaining about
>>> converting void* to function pointer. Searching for this error I
>>> found a explanation that C99 or C11 I don't recall makes this
>>> distinction and this conversion is not guaranteed. The solution
>>> proposed on stack overflow was storing a dummy function pointer
>>> instead of void*, lets say void (PF)(void) and then cast this
>>> dummy function pointer to the final type. I decided just ignore
>>> the warning in my code. The justification was that void* could
>>> have different size than function pointers.
>>
>> C does not define the behavior of converting between object pointer
>> types and function pointer types (except for the special case of a
>> null pointer constant). I believe that's been the case since C90.
>>
>> The section of the standard on pointer conversions just omits any
>> mention of conversions between object pointer types and function
>> pointer types. The section on cast operators, however, doesn't say
>> that such a conversion is a constraint violation. I find this odd.
>> (I think we had a long and inconclusive discussion about this a
>> while ago.)
>>
>> It does allow conversions of function pointer types to and from
>> integers, and integers to and from object pointer types, but the
>> results are implementation-defined in most cases.
>>
>> And yes, the rationale is that void* has to be big enough to hold
>> any object pointer value, but function pointers might be bigger
>> than object pointers (more precisely, they might not be convertible
>> without loss of information).
>
> It isn't made a constraint violation because in many cases it does
> work and being able to do it is useful.
>
> It isn't defined to work, because on some machines it just can't
> be done (especially if they are different sizes or to different
> memory spaces like a Harvard Machine)
Also to give implementations the freedom to reject programs that
contain such conversions if they choose to do so, even if there
might be an obvious way to implement it.