home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
- From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
- Newsgroups: comp.os.linux
- Subject: Re: GCC 2.2.2d bug
- Message-ID: <1992Sep7.141948.20280@klaava.Helsinki.FI>
- Date: 7 Sep 92 14:19:48 GMT
- References: <1992Sep4.210545.36040@uservx.plk.af.mil> <8eToqB1w164w@kryton.UUCP>
- Organization: University of Helsinki
- Lines: 49
-
- When discussing ``p = p++'', system@kryton.UUCP (Scott Beckstead)
- writes:
- >Indeed very poor code. The final result will be 11. because even if
- >the assign works properly the increment will still happen.
-
- Wrong. There is no single correct answer (except a core dump). The
- assignment and increment can happen in any order, since they occur in
- the same interval between two sequence points. Go read the comp.lang.c
- FAQ, the standard, and any decent book on C about sequence points.
- Please. I have included an extract of the C FAQ, in order to kill this
- thread. It is irrelevant to Linux, and IMHO doesn't need any more
- posts, since the subject has already been discussed to death in other
- newsgroups (c.l.c. and comp.std.c).
-
- --
- Lars.Wirzenius@helsinki.fi
-
- From the C FAQ:
-
- 3.1: Under my compiler, the code
-
- int i = 7;
- printf("%d\n", i++ * i++);
-
- prints 49. Regardless of the order of evaluation, shouldn't it
- print 56?
-
- A: Although the postincrement and postdecrement operators ++ and --
- perform the operations after yielding the former value, the
- implication of "after" is often misunderstood. It is _not_
- guaranteed that the operation is performed immediately after
- giving up the previous value and before any other part of the
- expression is evaluated. It is merely guaranteed that the
- update will be performed sometime before the expression is
- considered "finished" (before the next "sequence point," in ANSI
- C's terminology). In the example, the compiler chose to
- multiply the previous value by itself and to perform both
- increments afterwards.
-
- The behavior of code which contains multiple, ambiguous side
- effects has always been undefined. Don't even try to find out
- how your compiler implements such things (contrary to the ill-
- advised exercises in many C textbooks); as K&R wisely point out,
- "if you don't know _how_ they are done on various machines, that
- innocence may help to protect you."
-
- References: K&R I Sec. 2.12 p. 50; K&R II Sec. 2.12 p. 54; ANSI
- Sec. 3.3 p. 39; CT&P Sec. 3.7 p. 47; PCS Sec. 9.5 pp. 120-1.
- (Ignore H&S Sec. 7.12 pp. 190-1, which is obsolete.)
-