register variable???

Norman Diamond diamond at csl.sony.co.jp
Wed Sep 13 12:45:17 AEST 1989


In article <30585 at srcsip.UUCP> sklee at srcsip.UUCP () writes:

>  i = 0;
>  while (i < 4)  list[i] = list[++i];
>
>... if I define "int i" instead of "register int i",
>the result is [different]

This question seems to come up twice a week now.

list    [    i    ]    =      list    [    ++    i    ]
        [b]  [a]       [last] [4]     [3]  [2]   [1]

There are several possible sequences of evaluation.
Operation [1] (fetching value of i) must precede [2] (increment).
Operation [2] must precede [3] (array indexing) because it is a
pre-increment operation.
Operation [3] must precede [4] (fetching value of array element).
Operation [a] (fetching value of i) must precede [b] (array indexing).
All of the above must precede the assignment [last].

Notice what isn't specified?  Does operation [a] come before or after
operation [2]?  [a] could even come before [1] or after [4].

On the right-hand side, the rules of the language specify that
list[++i] uses the new value of i.  On the left-hand side, the rules
do not specify whether list[i] uses the old or new value of i.

If you were lucky, the compiler would call a random number generator
to decide whether to use the old value of i or the new value.
Unfortunately it chose one way for "register" and the other way
for non-"register".  Therefore you were confused, and you thought
that "register" makes a difference.  It does not.

--
-- 
Norman Diamond, Sony Corporation (diamond at ws.sony.junet)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.



More information about the Comp.lang.c mailing list