Order of evalution of expressions.
Bruce Worden
bruce at seismo.gps.caltech.edu
Tue Sep 25 10:06:21 AEST 1990
In article <6398 at castle.ed.ac.uk> elee24 at castle.ed.ac.uk (H Bruce) writes:
>Is the line
>
>value = *ptr - *ptr++;
>
>sensible C ?
As others have pointed out, it is not; the behavior is undefined and
therefore may be implementation dependent.
>If not what is the fastest way of computing this type of expression ?
>Would the following lines be optimized by a compiler (so that value is
>not loaded twice) ?
>value = *ptr;
>value -= *ptr++;
Remember, the increment takes place after the value is used, so you are
effectively subtracting *ptr from itself. I suspect that what you want
to do is:
value = ptr[0] - ptr[1];
ptr++;
As was discussed here a couple of weeks ago, this form should be as
efficient as the form:
value = *ptr - *(ptr+1);
ptr++;
The first form is, I think, clearer.
--------------------------------------------------------------------------
C. Bruce Worden bruce at seismo.gps.caltech.edu
252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125
More information about the Comp.lang.c
mailing list