From: Andy Isaacson <adi@hexapodia.org>
To: Marc Singer <elf@buici.com>
Cc: Marcel Holtmann <marcel@holtmann.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] spi
Date: Tue, 9 Aug 2005 10:54:34 -0700 [thread overview]
Message-ID: <20050809175434.GA23389@hexapodia.org> (raw)
In-Reply-To: <20050808174721.GA2853@buici.com>
On Mon, Aug 08, 2005 at 10:47:21AM -0700, Marc Singer wrote:
> On Mon, Aug 08, 2005 at 07:35:36PM +0200, Marcel Holtmann wrote:
> > > > + if (NULL == dev || NULL == driver) {
> > >
> > > Put the variable on the left side, gcc will complain if you incorrectly
> > > put a "=" instead of a "==" here, which is all that you are defending
> > > against with this style.
> >
> > I think in this case the preferred way is
> >
> > if (!dev || !driver) {
> >
>
> That's not a guaranteed equivalence in the C standard. Null pointers
> may not be zero. I don't think we have any targets that work this
> way, however there is nothing wrong with explicitly testing for NULL.
False. The expression "!x" is precisely equivalent to "x==0", no
matter what the type of x is. [1] And furthermore, NULL==0. [2]
Ergo, "NULL == dev" and "!dev" are defined to be equivalent.
What you're confused about is that the *representation* of a null
pointer constant does not necessarily have to be all-bits-zero. That
is, the following code fragment might print something on a
standard-compliant C implementation:
void *a = 0; unsigned char *p = (unsigned char *)&a;
int i;
for(i=0; i<sizeof(a); i++)
if(p[i] != 0) printf("p[%d] = %02x!\n", i, p[i]);
That does not change the fact that the source-code fragment "NULL" is
defined to be equivalent to the source-code fragment "0". Simply the
compiler must do whatever trickery necessary to ensure the correct
values get generated in the object code for my above hypothetical
architecture when I say "void *a = 0;".
This is very similar to how floating point is handled in the abstract
machine definition of the standard. Consider a weird FP implementation
where 0.0 has a not-all-bits-zero representation, and change 'a' in my
example above to type 'double'. Just because 0.0 is stored as the bit
pattern 0x8000000000000000 does not mean that I have to write something
other than "double a = 0;"!
And furthermore, all of this was well-understood in the C89 standard;
it's not new in the C99 standard, although there are some
clarifications.
[1] ISO/IEC 9899:1999 6.5.3.3 Unary arithmetic operators
(5) The result of the logical negation operator ! is 0 if the value of
its operand compares unequal to 0, 1 if the value of its operand
compares equal to 0. The result has type int. The expression !E is
equivalent to (0==E).
[2] ISO/IEC 9899:1999 7.17
The following types and macros are defined in the standard header
<stddef.h>. ...
NULL
which expands to an implementation-defined null pointer constant...
and 6.3.2.3 Pointers
(3) An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.
-andy
next prev parent reply other threads:[~2005-08-09 17:54 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-10 20:01 [PATCH 3/3] kconfig: linux.pot for all arch Egry Gábor
2005-08-08 9:12 ` [PATCH] spi dmitry pervushin
2005-08-08 10:41 ` Jiri Slaby
2005-08-08 13:16 ` Mark Underwood
2005-08-08 16:41 ` dmitry pervushin
2005-08-08 18:51 ` Mark Underwood
2005-08-08 14:55 ` Greg KH
2005-08-08 17:35 ` Marcel Holtmann
2005-08-08 17:47 ` Marc Singer
2005-08-09 17:54 ` Andy Isaacson [this message]
2005-08-09 19:05 ` Marc Singer
2005-08-09 19:29 ` Andy Isaacson
2005-08-15 7:51 ` Denis Vlasenko
2005-08-08 22:58 ` Andrew Morton
2005-08-10 13:10 ` Pavel Machek
-- strict thread matches above, loose matches on Subject: below --
2005-08-08 23:07 david-b
2005-08-09 9:38 ` Mark Underwood
2005-09-26 11:12 SPI dmitry pervushin
2005-09-27 12:43 ` SPI Greg KH
2005-09-27 14:27 ` [spi-devel-general] SPI dmitry pervushin
2005-09-27 14:35 ` Greg KH
2005-09-27 14:49 ` dmitry pervushin
2005-09-27 14:54 ` Greg KH
2005-09-28 13:14 ` [PATCH] SPI dmitry pervushin
2005-09-30 17:59 David Brownell
2005-09-30 18:30 ` Vitaly Wool
2005-09-30 19:20 ` dpervushin
2005-10-03 4:56 David Brownell
2005-10-03 5:01 David Brownell
2005-10-03 6:20 ` Vitaly Wool
2005-10-03 16:26 David Brownell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050809175434.GA23389@hexapodia.org \
--to=adi@hexapodia.org \
--cc=elf@buici.com \
--cc=linux-kernel@vger.kernel.org \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox