For the C++ program show on the next page, answer the questions that follow

Question

For the C++ program show on the next page, answer the questions that follow. The names g, j, x, and y are declare multiple times. So Distinguish uses of these names using line numbers where they were declare as subscripts.
a. At point A in the program, what names are in scope?
b. At point A in the program, what variables are live?
c. At point B in the program, what names are in scope?
d. At point B in the program, what variables are live?
Code for Problem III.

int g;
int h;
int bar(int x, int y) {
int f = x + y;
int g = 2 * h;
while (f < 0) {
int x = f - 1;
int y = g - 1;
g = x + y;
f--;
}
// Point A
f++;
int z = f * g;
return z;
}
int j = g + h;
void print(int v);
int foo(int a) {
int x = a * 2;
// Point B
int r = bar(a, x);
return r;
}
int main() {
int j = 3;
int k = 4;
int m = foo(j + k);
print(m);
return 0;
}

Summary

  • {x, y, g, h, z, f} are the variables that have a scope at point A.
  • ‘g’ and ‘h’ are the variables that are live under point A.
  •  {g, h, a, x, r, j} are the variables that can be accessed under point B.
  •  {g, h, j} are the variables that are live under point B.

Explanation

Q) At point A in the program, what names are in scope?

  • At line numbers, 16, 17, and 18 there are total of three statements at points A which are in the function naming as bar().
  • So We will call the variable in scope if they can be accessed at the point.
  • ‘g’ and ‘h’ are declare global so they can be accessed anywhere throughout the program.
  •  ‘f’ is in the local scope as it is declared inside the function. so ‘z’ also declares here.
  • ‘x’ and ‘y’ are passed as arguments to the function so that they are in the local scope.
  •  Hence {x, y, g, h, z, f} is the variable that can be accessed under point A

 

Q) At point A in the program, what variables are live?

  • Live variable means the variable which holds the value which can be use in the program.
  •  ‘z’ variable is returned when there is a function call, on line number 18.
  •  At point A there are two variables naming as ‘g’ and ‘h’. And they are live which will be used in the program.
  • ‘f’, ‘x’, and ‘y’ are in the local scope that’s why they cannot be used in the program.
  • Hence the variable ‘g’ and ‘h’ are live on point A.

 

Q) At point B in the program, what names are in scope?

  •  On Point B, on line 28 and 29 there is 2 statements in the function.
  • That statements are in the function foo()
  • Here we passed a parameter with the value of ‘a’ so that it has local scope in point B.
  •  So next global variables can e accessed at any point of the program. Global variables are ‘g’ and ‘h’.
  •  Variable ‘r’ also declares so that it can have a local scope, along with the variable ‘X’ which is in the function foo.
  •  ‘j’ declares {g, h, a, x, r, j} are the variables that can be accessed on point B.
  •  outside the function such that it is non-local scope.

Q) At point B in the program, what variables are live?

  • Live variable means the variable which holds the value which can be used in the program.
  •  So When we called a function foo it returned a ‘r’ which is variable. So that it can be on line no 29.
  •  At point B there are two variables naming as ‘g’ and ‘h’. And they are live which will be used in the program.
  •  Whereas ‘j’ variable can also be use in program.
  •  Hence variables {g, h, j} are live under the point B.

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *