home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12496 < prev    next >
Encoding:
Text File  |  1992-08-18  |  3.2 KB  |  74 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Paranoia, side effects, and optimizing compilers
  5. Date: 18 Aug 1992 18:47:30 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 61
  8. Message-ID: <25569@dog.ee.lbl.gov>
  9. References: <DAVIS.92Aug14120003@pacific.mps.ohio-state.edu> <BszL5o.B0s@pgroup.com> <9222819.26380@mulga.cs.mu.OZ.AU>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <DAVIS.92Aug14120003@pacific.mps.ohio-state.edu>
  14. davis@pacific.mps.ohio-state.edu ("John E. Davis") writes:
  15. >     unsigned char *p;   
  16. >     while (1)
  17. >       {
  18. >          p = CLine->data + Point;
  19. >          if (*p > ' ') break;
  20. >          del();
  21. >       }
  22.  
  23. Just as a side note, I would probably rewrite this as:
  24.  
  25.     while (CLine->data[Point] <= ' ')
  26.         del();
  27.  
  28. >Here CLine->data is a pointer of same type as p and Point is an integer.
  29. >del() is a routine that has that might possibly do a realloc on
  30. >CLine->data so p might change in the loop.
  31.  
  32. >Would an optimizing compiler asssume that p will not change in the loop,
  33. >evaluate it once and assume it is fixed in the loop?  [or should I make
  34. >p `volatile']
  35.  
  36. Short answer: if so, it would be broken.
  37.  
  38. In article <BszL5o.B0s@pgroup.com> lfm@pgroup.com (Larry Meadows) gets
  39. this right.  He goes on to say that
  40.  
  41. >volatile is only necessary for variables that might change due to events
  42. >that aren't explicitly specified (e.g., shared memory multiprocessor
  43. >systems, setjmp-longjmp, interrupt handlers, external hardware).
  44.  
  45. More precisely, those that might change *or be accessed*: compilers
  46. can move or eliminate assignments to nonvolatile variables, provided
  47. this is not detectable through proper C language constructs.  Volatile
  48. variables are (aside from longjmp and signals, where the standard wimps
  49. out :-) ) those that are used in some way ``outside'' the language.
  50. This includes things like programs or hardware that scan shared memory,
  51. as well as those that change them.
  52.  
  53. In article <9222819.26380@mulga.cs.mu.OZ.AU> baillie@cs.mu.OZ.AU
  54. (Stephen Baillie) writes:
  55. >Strangely enough, it depends on the compiler.  For example, Microsoft's
  56. >C/C++ 7 gives one two options on this - /Oa to assume that there is no
  57. >aliasing (function calls do not change variables) and /Ow for weak
  58. >aliasing (function calls may change variables, but things in the code don't)
  59.  
  60. These options are nonconformant.  Microsoft has chosen to give you
  61. several ways to say: ``compile a language that resembles C, but is not
  62. actually C'', because programs written in those other languages are
  63. easier to compile.  As a result, if you write programs in those other
  64. languages---whether deliberately or accidentally---Micosoft's not-
  65. quite-C version of the compiler may produce faster object code.
  66.  
  67. (Whether these `weakened' version of C are a Good Thing depends on
  68. one's point of view.  It is far easier to get in trouble with them; but
  69. on the other hand, it is far easier to write a compiler that produces
  70. faster code with them.)
  71. -- 
  72. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  73. Berkeley, CA        Domain:    torek@ee.lbl.gov
  74.