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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!pgroup!lfm
  3. From: lfm@pgroup.com (Larry Meadows)
  4. Subject: Re: Paranoia, side effects, and optimizing compilers
  5. Message-ID: <BszL5o.B0s@pgroup.com>
  6. Date: Fri, 14 Aug 1992 18:39:23 GMT
  7. References: <DAVIS.92Aug14120003@pacific.mps.ohio-state.edu>
  8. Organization: The Portland Group, Portland, OR
  9. Lines: 49
  10.  
  11. In article <DAVIS.92Aug14120003@pacific.mps.ohio-state.edu> davis@pacific.mps.ohio-state.edu  (John E. Davis) writes:
  12. >Hi,
  13. >
  14. >   Consider:
  15. >
  16. >      unsigned char *p;   
  17. >   
  18. >      while (1)
  19. >        {
  20. >           p = CLine->data + Point;
  21. >           if (*p > ' ') break;
  22. >           del();
  23. >        }
  24. >
  25. >Here CLine->data is a pointer of same type as p and Point is an integer.
  26. >del() is a routine that has that might possibly do a realloc on
  27. >CLine->data so p might change in the loop.
  28. >
  29. >Would an optimizing compiler asssume that p will not change in the loop,
  30. >evaluate it once and assume it is fixed in the loop?  In this case, I would
  31. >want to declare it as volatile but I'd rather not worry about these issues.
  32. >Or should I?
  33.  
  34. As long as CLine is external (or can be accessed via external
  35. variables), the compiler must assume (in the absence of fancy
  36. interprocedural analysis) that any function call can change its value,
  37. therefore p cannot be invariant in the loop.  Consider a more simple
  38. example:
  39.  
  40. main()
  41. {
  42.     extern int i;
  43.     int j;
  44.  
  45.     for (;;) {
  46.     foo();
  47.     if (i == 0) break;
  48.     }
  49. }
  50.  
  51. Since i is external, its value can be changed by foo(), so it must be
  52. reloaded from memory after every call to foo().
  53.  
  54. volatile is only necessary for variables that might change due to events
  55. that aren't explicitly specified (e.g., shared memory multiprocessor
  56. systems, setjmp-longjmp, interrupt handlers, external hardware).
  57. -- 
  58. Larry Meadows        The Portland Group
  59. lfm@pgroup.com
  60.