home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!pgroup!lfm
- From: lfm@pgroup.com (Larry Meadows)
- Subject: Re: Paranoia, side effects, and optimizing compilers
- Message-ID: <BszL5o.B0s@pgroup.com>
- Date: Fri, 14 Aug 1992 18:39:23 GMT
- References: <DAVIS.92Aug14120003@pacific.mps.ohio-state.edu>
- Organization: The Portland Group, Portland, OR
- Lines: 49
-
- In article <DAVIS.92Aug14120003@pacific.mps.ohio-state.edu> davis@pacific.mps.ohio-state.edu (John E. Davis) writes:
- >Hi,
- >
- > Consider:
- >
- > unsigned char *p;
- >
- > while (1)
- > {
- > p = CLine->data + Point;
- > if (*p > ' ') break;
- > del();
- > }
- >
- >Here CLine->data is a pointer of same type as p and Point is an integer.
- >del() is a routine that has that might possibly do a realloc on
- >CLine->data so p might change in the loop.
- >
- >Would an optimizing compiler asssume that p will not change in the loop,
- >evaluate it once and assume it is fixed in the loop? In this case, I would
- >want to declare it as volatile but I'd rather not worry about these issues.
- >Or should I?
-
- As long as CLine is external (or can be accessed via external
- variables), the compiler must assume (in the absence of fancy
- interprocedural analysis) that any function call can change its value,
- therefore p cannot be invariant in the loop. Consider a more simple
- example:
-
- main()
- {
- extern int i;
- int j;
-
- for (;;) {
- foo();
- if (i == 0) break;
- }
- }
-
- Since i is external, its value can be changed by foo(), so it must be
- reloaded from memory after every call to foo().
-
- volatile is only necessary for variables that might change due to events
- that aren't explicitly specified (e.g., shared memory multiprocessor
- systems, setjmp-longjmp, interrupt handlers, external hardware).
- --
- Larry Meadows The Portland Group
- lfm@pgroup.com
-