home *** CD-ROM | disk | FTP | other *** search
- Nntp-Posting-Host: solva.ifi.uio.no
- Newsgroups: comp.lang.c
- Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
- From: jar@solva.ifi.uio.no (Jo Are Rosland)
- Subject: Re: Question to test general C knowledge
- In-Reply-To: volpe@bart.NoSubdomain.NoDomain's message of Sun, 13 Dec 1992 05:14:42 GMT
- Message-ID: <JAR.92Dec13235454@solva.ifi.uio.no>
- X-Md4-Signature: 26f0d8e13c00a2845a2e57509e2389f6
- Sender: jar@ifi.uio.no (Jo Are Rosland)
- Organization: Dept. of Informatics, University of Oslo, Norway
- References: <Bz0A46.Cvu@watserv1.uwaterloo.ca> <19980@ksr.com>
- <1992Dec11.180939.20726@crd.ge.com>
- <1992Dec12.230520.771@thunder.mcrcim.mcgill.edu>
- <1992Dec13.051442.1867@crd.ge.com>
- Date: Sun, 13 Dec 1992 22:54:54 GMT
- Lines: 47
- Originator: jar@solva.ifi.uio.no
-
- In article <1992Dec13.051442.1867@crd.ge.com> volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe) writes:
- |>
- |> Based on a sequence like
- |>
- |> ! compute needed values
- |> addl3 _i,$1,r0 ! compute value of ++i
- |> ! sequence point; do accumulated side-effects
- |> movl r0,_i ! store value of ++i into i
- |> incl _i ! increment i
- |>
- |> Thus we see the wisdom of ANSI's rule :-)
-
- Umm. I question whether it would be legal for the compiler to emit
- such code. The reason is that the value of the "++i" expression is
- "the value of i after the assignment". The first assembly statement
- computes this value as "3". However, the "value of i after the assignment"
- is determined by the result of the "incl _i" statement, and that
- value is 4, which contradicts the definition of the value of the "++i"
- expression.
-
- An expression in C has a return value. Some expressions also have
- side effects. The _return_value_ of "++i" is 3 (given 2 as the
- original value), while the _side_effect_ is to increment "i". Trouble
- is, the compiler doesn't have to compute the return value and execute
- the side effect at the same time.
-
- The following is the likely way the compiler will operate:
-
- - The return value of "++i" as well as the address of "i" will have to
- be computed before the "=" is performed.
-
- - The side effect of incrementing "i" can be performed before, during
- or after these computations.
-
- - The compiler can perform the incrementing of "i" by assigning it the
- return value of "++i" at some point after it's computed, or by simply
- incrementing it.
-
- But, due to the "undefinedness" of the construct, none of these are
- guarantees. The compiler could opt to do something entirely
- unexpected, like bringing about world peace.
-
- Hope this helps,
-
- --
- Jo Are Rosland
- jar@ifi.uio.no
-