From: Sean Hunter <sean@uncarved.com>
To: Jan Hudec <bulb@ucw.cz>, linux-kernel@vger.kernel.org
Subject: Re: [BUG] Bad #define, nonportable C, missing {}
Date: Wed, 21 Nov 2001 14:24:37 +0000 [thread overview]
Message-ID: <20011121142437.A24408@dev.sportingbet.com> (raw)
In-Reply-To: <01112112401703.01961@nemo> <3BFB9FAE.DB9B6003@dexterus.com> <20011121143738.D2196@artax.karlin.mff.cuni.cz>
In-Reply-To: <20011121143738.D2196@artax.karlin.mff.cuni.cz>; from bulb@ucw.cz on Wed, Nov 21, 2001 at 02:37:38PM +0100
The great thing about the C standard is that you don't have to know, or guess,
or remember. I respectfully suggest that all of those who are interested in
this topic buy a copy of K&R Second Edition (ISBN 0-13-110370-9), and read
chapter 2, particularly section 2.12 "Precedence and order of Evaluation".
And take this facinating topic off-line.
Sean
On Wed, Nov 21, 2001 at 02:37:38PM +0100, Jan Hudec wrote:
> > > *a++ = byte_rev[*a]
> > It looks perferctly okay to me. Anyway, whenever would you listen to a
> > C++ book talking about good C coding :p
>
> AFAIK the ANSI C specification explicitely claims, that it's not defined.
> The trick is, that the specification explicitly allows the compiler to
> choose wether it does the inc/dec right after/before the fetch, or at the
> begin/end of evaluation. Thus the second reference to a might return the
> original or incremented value at compiler's will.
>
> > Go read up on C operator precedence. Unary ++ comes before %, so if we
> > rewrite the #define to make it more "readable" it would be #define
> > MODINC(x,y) (x = (x+1) % y)
>
> *NO*
> MODINC(x,y) (x = (x+1) % y)
> is correct and beaves as expected. Unfortunately:
> MODINC(x,y) (x = x++ % y)
> is a nonsence, because the evaluation is something like this
> x++ returns x
> x++ % y returns x % y
> x is assigned the result and it's incremented IN UNDEFINED ORDER!!!
> AFAIK the ANSI C spec explicitly undefines the order.
>
> > > *a++ = byte_rev[*a];
> > C std says *always* evaluate from right to left for = operators, so this
> > will always make perfect sense.
> Yes, this should make perfect sense, but I fear the spec talks about
> operand used twice, once with side-efect generaly. So to be ANSI C
> correct, it's not good.
>
> -------------------------------------------------------------------------------
> - Jan Hudec `Bulb' <bulb@ucw.cz>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>
next prev parent reply other threads:[~2001-11-21 14:26 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-11-21 12:40 [BUG] Bad #define, nonportable C, missing {} vda
2001-11-21 11:10 ` Andreas Schwab
2001-11-21 11:16 ` Tim Waugh
2001-11-21 12:31 ` Ragnar Hojland Espinosa
2001-11-21 13:40 ` Jan Hudec
2001-11-21 14:19 ` Andreas Schwab
2001-11-21 14:52 ` Alexander Viro
2001-11-21 18:23 ` Neil Booth
2001-11-21 12:35 ` Vincent Sweeney
2001-11-21 13:37 ` Jan Hudec
2001-11-21 13:52 ` Mathijs Mohlmann
2001-11-21 17:12 ` vda
2001-11-26 20:28 ` Alan Cox
2001-11-27 18:03 ` vda
2001-11-27 18:38 ` Andreas Dilger
2001-11-28 13:19 ` vda
2001-11-21 14:12 ` Richard B. Johnson
2001-11-21 14:33 ` Eric W. Biederman
2001-11-21 14:56 ` Alexander Viro
2001-11-21 14:59 ` Andreas Schwab
2001-11-21 15:48 ` Momchil Velikov
2001-11-21 16:52 ` vda
2001-11-21 14:24 ` Sean Hunter [this message]
2001-11-21 14:25 ` Andreas Schwab
2001-11-22 20:43 ` Chris Gray
2001-11-22 4:24 ` Stevie O
2001-11-22 11:46 ` Horst von Brand
2001-11-22 12:03 ` Alexander Viro
2001-11-22 20:08 ` J.A. Magallon
[not found] ` <01112311540300.00886@manta>
2001-11-23 14:43 ` J.A. Magallon
-- strict thread matches above, loose matches on Subject: below --
2001-11-27 19:03 Nathan Myers
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=20011121142437.A24408@dev.sportingbet.com \
--to=sean@uncarved.com \
--cc=bulb@ucw.cz \
--cc=linux-kernel@vger.kernel.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