home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18310 < prev    next >
Encoding:
Text File  |  1992-12-14  |  2.6 KB  |  66 lines

  1. Nntp-Posting-Host: solva.ifi.uio.no
  2. Newsgroups: comp.lang.c
  3. Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
  4. From: jar@solva.ifi.uio.no (Jo Are Rosland)
  5. Subject: Re: Question to test general C knowledge
  6. In-Reply-To: volpe@bart.NoSubdomain.NoDomain's message of Sun, 13 Dec 1992 05:14:42 GMT
  7. Message-ID: <JAR.92Dec13235454@solva.ifi.uio.no>
  8. X-Md4-Signature: 26f0d8e13c00a2845a2e57509e2389f6
  9. Sender: jar@ifi.uio.no (Jo Are Rosland)
  10. Organization: Dept. of Informatics, University of Oslo, Norway
  11. References: <Bz0A46.Cvu@watserv1.uwaterloo.ca> <19980@ksr.com>
  12.     <1992Dec11.180939.20726@crd.ge.com>
  13.     <1992Dec12.230520.771@thunder.mcrcim.mcgill.edu>
  14.     <1992Dec13.051442.1867@crd.ge.com>
  15. Date: Sun, 13 Dec 1992 22:54:54 GMT
  16. Lines: 47
  17. Originator: jar@solva.ifi.uio.no
  18.  
  19. In article <1992Dec13.051442.1867@crd.ge.com> volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe) writes:
  20.    |> 
  21.    |> Based on a sequence like
  22.    |> 
  23.    |>         ! compute needed values
  24.    |>         addl3   _i,$1,r0        ! compute value of ++i
  25.    |>         ! sequence point; do accumulated side-effects
  26.    |>         movl    r0,_i           ! store value of ++i into i
  27.    |>         incl    _i              ! increment i
  28.    |> 
  29.    |> Thus we see the wisdom of ANSI's rule :-)
  30.  
  31.    Umm. I question whether it would be legal for the compiler to emit
  32.    such code. The reason is that the value of the "++i" expression is
  33.    "the value of i after the assignment". The first assembly statement
  34.    computes this value as "3". However, the "value of i after the assignment"
  35.    is determined by the result of the "incl _i" statement, and that 
  36.    value is 4, which contradicts the definition of the value of the "++i"
  37.    expression. 
  38.  
  39. An expression in C has a return value.  Some expressions also have
  40. side effects.  The _return_value_ of "++i" is 3 (given 2 as the
  41. original value), while the _side_effect_ is to increment "i".  Trouble
  42. is, the compiler doesn't have to compute the return value and execute
  43. the side effect at the same time.
  44.  
  45. The following is the likely way the compiler will operate:
  46.  
  47. - The return value of "++i" as well as the address of "i" will have to
  48.   be computed before the "=" is performed.
  49.  
  50. - The side effect of incrementing "i" can be performed before, during
  51.   or after these computations.
  52.  
  53. - The compiler can perform the incrementing of "i" by assigning it the
  54.   return value of "++i" at some point after it's computed, or by simply
  55.   incrementing it.
  56.  
  57. But, due to the "undefinedness" of the construct, none of these are
  58. guarantees.  The compiler could opt to do something entirely
  59. unexpected, like bringing about world peace.
  60.  
  61. Hope this helps,
  62.  
  63. --
  64. Jo Are Rosland
  65. jar@ifi.uio.no
  66.