7.1 – For the languages C and Java, give as precise binding times as you can for the following attributes. Explain your answers.
a) The maximum number of significant digits in a real number
Static because the max number will be an integer and it is only bound after to a variable.
b) The meaning of char
Dynamic;this is because it is not bound to any value.
c) The size of an array variable
Static;this is because the size will be bound to a variable.
d) The size of an array parameter
Dynamic;this is because the size is not bound until after translation.
e) The location of a local variable
Dynamic;this is because the location was set during execution.
f) The value of a constant
Static;this is because the value will be bound to some variable.
g) The location of a function
Dynamic;this is because it is not bound to any variable.
7.7 As noted in this chapter, C and C++ make a distinction between a Declaration and a Definition. Which of the following are declarations only and which are definitions?
a) char * name;
Declaration
b) typedef int * IntPtr;
Declaration
c) struct rec;
Declaration
d) int gcd(int, int);
Definition
e) double y;
Definition
f) extern int z;
Declaration
7.8 – Using the first organization of the symbol table described in the text (a single simple table), show the symbol table for the following C program at the three points indicated by the comments (a) using the lexical scope and (b) using dynamic scope. What does the program print using each kind of scope rule?
Output: The program will print the integers 3 and 1.
#include <stdio.h
inta,b;
int p(){int a, p;
/* point 1 */
a = 0; b = 1; p = 2;
return p;
}
void print(){
printf("%d\n%d\n",a,b);
}
void q (){
int b;
/* point 2 */
a = 3; b = 4;
print();
}
main(){
/* point 3 */
a = p();
q();
}
a) Using the lexical scope, what does the program print using each kind of scope rule?
Due to lexical scope functioning locally, each code will only return the functions that are declared within the scope point 1 which will return p and results in 3. void print() will not print anything because a and be are not declared point 2 will only print b because b is the only thing declared in the scope. Lastly, the main will only return q() because a is not declared in the scope.
b)Using dynamic scope. What does the program print?
Dynamic scope runs globally so it will return all variables that are declared form the beginning. The program will also print 1.
7.14 – The following program prints two integer values: the first value typically is garbage (or possibly 0, if you are executing inside a debugger or other controlled environment), but the second value might be 2. Explain why.
#include <stdio.h
void p(void){
int y;
printf("%d\n", y);
y = 2;
}
main(){
p(); p();
return 0;
}
The first value is garbage because y was printed without an initial value and so what it would print does not have a value. The second value will be 2 because at this point y has an initial value of 2.
7.16 Explain the difference between aliasing and side effects.
Aliasing refers to having two different 'names' assigned to the same "place". An example is with pointers. With two pointers assigned to the same place those two pointers reference the same part of memory. If you change the value stored there through one of the pointers, you get the changed value. The idea is that there is only one place in memory to which the 'names' all refer. This is the side effect of aliasing.
7.24 a)Which of the following C expressions are l-values? Which are not? Why? (assume x is an int variable and y is an int* variable.)
(1) x+2
(2) &x
(3) *&x
(4) &x + 2
(5) * (&x + 2)
(6) &*y
b) Is it possible for a C expression to be an l-value but not an r-value? Explain.
c) Is &(&z) ever legal in C? Explain
7.29 A single-assignment variable is a variable whose value can be computed at any point but that can be assigned to only once, so that it must remain constant once it is computed. Discuss the usefulness of this generalization of the idea of constant declarations in C, C++, Java, or Ada. How does this concept relate to that of dynamic constant, as discussed in Section 7.6?
7.33 – Here is a legal C program with a function variable:
(1) #include <stdio.h
(2) int gcd( int u, int v){
(3) if (v == 0) return u;
(4) else return gcd(v, u % v);
(5) }
(6) int (*fun_var)(int,int) = &gcd;
(7) main(){
(8)printf("%d\n", (*fun_var)(15,10));
(9)return 0;
(10) }
Compare lines 6 and 8 of this code to the equivalent code of Figure 7.45 (lines 1 and 3). Why is this version of the code permitted? Why is it not required?
The code is permitted because it has a new int pointer *fun_var pointer to two int variables that are not declared yet and since in the print the two int are given a value it computes. Since there were already a u and a v that were declared that could be given the values the new pointer was not required.