Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Issues with setting variables to true or false: how does the compiler understands it Date: Sat, 24 Oct 2015 20:33:48 -0700 Organization: None to speak of Lines: 48 Message-ID: References: <562BAA43.8090700@verizon.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="9695"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gJquN1+neao+pMckbPZcv" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:y5HSRhN67hoq+GySaxo2BF/Y6bU= sha1:oAQKCv5+rZeEJK2zHaG/E6/0BXQ= Xref: csiph.com comp.lang.c:73750 supercat@casperkitty.com writes: > On Saturday, October 24, 2015 at 6:29:17 PM UTC-5, Keith Thompson wrote: >> > When testing a pointer against null, one isn't really interested in >> > comparing the pointer matches a null pointer, but whether it's valid. >> >> No, if I write `if (ptr != NULL)`, I'm testing whether ptr is unequal to >> the null pointer. If it's equal to NULL, it's because that's the most >> recent value I stored in it. I might also be *interested* in knowing >> whether it's valid, but I have no way of testing that, so I don't >> bother. > > If code written in Standard C is to have any hope of working right, the > pointer must either be valid or null. Attempting an rvalue conversion on > an invalid pointer would invoke Undefined Behavior even before the > comparison could be performed, and even in compilers where pointers > continue to identify storage locations and the relationships among them > regardless of whether anything owns the storage in question, there is > seldom much call to compare a possibly-invalid pointer against null. It > is far more common that a pointer is guaranteed to either be valid or > null. All that is true, and none if it refutes anything I've said. Was it intended to? >> > The fact that there is only one testable representation for a valid >> > pointer is coincidental. > >> I disagree; there's nothing coincidental about it. `if (ptr)` and >> `if (ptr != NULL)` are semantically identical. The latter expresses >> my intent more clearly. > > Different people parse things differently; I consider both to be idiomatic > and don't have any problem with either, though I never use the NULL macro > [I don't even remember offhand whether it's in stdlib.h or elsewhere]. I also consider both to be idiomatic. I simply prefer the more explicit form, for reasons I've explained. It's defined in , , , , , , and . is in some sense the primary location, but it's defined in every standard header that uses null pointers. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"