[MPlayer-dev-eng] [PATCH] cast null pointers
Christopher Key
cjk32 at cam.ac.uk
Thu Aug 12 20:21:28 CEST 2010
Reimar Döffinger wrote:
> On Thu, Aug 12, 2010 at 06:23:11PM +0100, Christopher Key wrote:
>
>> Reimar Döffinger wrote:
>>
>>> On Thu, Aug 12, 2010 at 06:54:35PM +0200, Reimar Döffinger wrote:
>>>
>>>
>>>> On Thu, Aug 12, 2010 at 04:56:49PM +0100, Edd Barrett wrote:
>>>>
>>>>
>>>>> Just a small stylistic note. Null pointers for execl should be casted to
>>>>> pointer types. Pointed out to me by naddy@ (http://c-faq.com/null/null2.html):
>>>>>
>>>>>
>>>> What a nonsense, the C99 standard (e.g. section 7.17) specifies NULL
>>>> as a "null pointer constant", so it is required to be a pointer type.
>>>> Some implementations may not conform to this, but unless you know
>>>> of a specific one where this actually causes issues this is just
>>>> obfuscation.
>>>>
>>>>
>>> Hm, I managed to get myself confused by the language.
>>> A "null pointer constant" is actually not required to be a "null
>>> pointer" and thus to be of pointer type.
>>> Seems somewhat silly to me, but well.
>>>
>>>
>> There's no guarantee that the various pointer types are interchangeable,
>> only that void* be large enough to represent any data pointer. In
>> pointer context, 0 will always work. In a non pointer context, there's
>> no guarantee that ((void *) 0) will do the correct thing, hence the
>> requirement for an explicit cast.
>>
>
> It would work for most cases, which would be quite an advantage already.
> I wouldn't mind so much if there was an actual _requirement_ for a cast,
> or at least if there was a way to make gcc print a warning if NULL was
> used in a non-pointer context without a cast, but as I see it there is
> simply no really maintainable solution.
>
>
To have the C99 spec require that NULL be ((void *) 0) because it fixes
broken code on some platforms seems like a very bad idea, such decisions
should be up to the implementation. The decision to have NULL as ((void
*) 0) in gcc does offend me, I'd rather have broken code fail early and
noisily and be fixed, but I will concede that it is practical. Having
gcc warn about use of NULL in a non-pointer context would certainly be
very useful.
--
Christopher Key
More information about the MPlayer-dev-eng
mailing list