The following problem illustrates the way memory aliasing cancause unexpected program behavior. Consider the followingprocedure to swap two values:1 /* Swap value x at xp with value y at yp */2 void swap(long *xp, long *yp)3 {4 *xp = *xp + *yp; /* x+y */5 *yp = *xp - *yp; /* x+y-y = x */6 *xp = *xp - *yp; /* x+y-x = y */7 }If this procedure is called with xp equal to yp , what effect will ithave?A second optimization blocker is due to function calls. As an example,consider the following two procedures:1 long f();23 long func1() {4 return f ()+ f ()+ f ()+ f () ;5 }67 long func2() {8 return 4*f();9 }It might seem at first that both compute the same result, but withfunc2 calling f only once, whereas func1 calls it four times. It istempting to generate code in the style of func2 when given func1 asthe source.Consider, however, the following code for f:1 long counter = 0;23 long f() {4 return counter++;5 }This function has a side effect—it modifies some part of the globalprogram state. Changing the number of times it gets called changesthe program behavior. In
The following problem illustrates the way memory aliasing can
cause unexpected
procedure to swap two values:
1 /* Swap value x at xp with value y at yp */
2 void swap(long *xp, long *yp)
3 {
4 *xp = *xp + *yp; /* x+y */
5 *yp = *xp - *yp; /* x+y-y = x */
6 *xp = *xp - *yp; /* x+y-x = y */
7 }
If this procedure is called with xp equal to yp , what effect will it
have?
A second optimization blocker is due to function calls. As an example,
consider the following two procedures:
1 long f();
2
3 long func1() {
4 return f ()+ f ()+ f ()+ f () ;
5 }
6
7 long func2() {
8 return 4*f();
9 }
It might seem at first that both compute the same result, but with
func2 calling f only once, whereas func1 calls it four times. It is
tempting to generate code in the style of func2 when given func1 as
the source.
Consider, however, the following code for f:
1 long counter = 0;
2
3 long f() {
4 return counter++;
5 }
This function has a side effect—it modifies some part of the global
program state. Changing the number of times it gets called changes
the program behavior. In
Trending now
This is a popular solution!
Step by step
Solved in 2 steps