Stephen Franchak
CMPS 344
Professor Jackowitz
Homework 04
Review Question
15) What is the general problem with static scoping?
The general problem with static scoping is that program designers are encouraged to use far more global variables and subprograms than are necessary. During the evolution of a regularly-used program, designers may disregard the restructuring required to retain the original structure of the program that restricted certain variable and subprogram access. The initial structure is often destroyed by local variables becoming global variables and subprograms becoming global rather than nested subprograms in languages that allow nested subprograms.
Ultimately, the final design may be contrived and no longer reflect the underlying conceptual design.
Problem Set
3) Write a simple assignment statement with one arithmetic operator in some language you know. For each component of the statement, list the various bindings that are required to determine the semantics when the statement is executed. For each binding, indicate the binding time used for the language.
Additionally, for some small but representative subprogram in some language you know, list the various bindings that are required to determine the semantics when the subprogram is executed. For each binding, indicate the binding time.
The following statement is an assignment statement with one arithmetic operator in Java. i = i + 1;
The various bindings that are required to determine the semantics of the statement when it is executed and the associated binding times for each component of the statement are as follows:
11) Consider the following skeletal C program: void fun1(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void) /* prototype */ void main() {
int a, b, c;
. . .
} void fun1(void) {
int b, c, d;
. . .
} void fun2(void) {
int c, d, e;
. . .
} void fun3(void) {
int d, e, f;
. . .
}
Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was defined. a.
main calls fun1; fun1 calls fun2; fun2 calls fun3. a – main b – fun1 c – fun2 d – fun3 e – fun3 f – fun3 b.
main calls fun1; fun1 calls fun3. a – main b – fun1 c – fun1 d – fun3 e – fun3 f – fun3 c.
main calls fun2; fun2 calls fun3; fun3 calls fun1.
a – main b – fun1 c – fun1 d – fun1 e – fun3 f – fun3 d.
main calls fun3; fun3 calls fun1. a – main b – fun1 c – fun1 d – fun1 e – fun3 f – fun3 e.
main calls fun1; fun1 calls fun3; fun3 calls fun2. a – main b – fun1 c – fun2 d – fun2 e – fun2 f – fun3 f.
main calls fun3; fun3 calls fun2; cun2 calls fun1. a – main b – fun1 c – fun1 d – fun1 e – fun2 f – fun3
Programming Exercises
5) Write a C function that includes the following sequence of statements: x = 21; int x; x = 42;
Run the program and explain the results. Rewrite the same code in C++ and Java and compare the results.
PE5.c main(void){
x = 21;
int x;
x = 42;
}
Result and explanation:
When attempting to compile PE5.c into the binary executable PE5 with the gcc compiler using the command “gcc -o PE5 PE5.c”, the following error messages are displayed:
PE5.c: In function 'main':
PE5.c:2: error: 'x' undeclared (first use in this function)
PE5.c:2: error: (Each undeclared identifier is reported only once
PE5.c:2: error: for each function it appears in.)
The compiler fails at compiling PE5.c. The compiler cannot compile the code because the C programming language uses static type binding by explicit declaration. Before a variable can be referenced, there must be a statement in the program that lists the variable name and specifies its type. Such a statement is called a declaration statement. A declaration statement binds a variable to a particular data type at compile time and to a storage cell at runtime. The statement x =
21; is invalid because the variable x has yet to be declared. The compiler cannot discern if the value being assigned to x is valid and, more importantly, a memory cell has not been allocated to store the value being assigned to x (and, in consequence, the variable does not have an associated memory address). In order to fix this program, one can add the statement int x; above the main function or move the statement int x; from its current position to a line above the statement x = 21; in the main function.
Similar results have been observed when trying to compile a program using the same statements in C++ and Java with the g++ and javac compilers, respectively. The reason for why these compilers failed to compile the code is the same as the reason for why gcc could not compile
PE5.c
PE5.cpp main(void){
x = 21;
int x;
x = 42;
}
Compiler error message
PE5.cpp: In function 'int main()':
PE5.cpp:4: error: 'x' was not declared in this scope
PE5.java public class PE5 {
public static void main(String[] argsv){
x = 21;
int x;
x = 42;
}
}
Compiler error message
PE5.java:3: cannot find symbol symbol : variable x location: class PE5
x = 21;
^
1 error
7) Write three functions in C or C++: one that declares a large array statically, one that declares the same large array on the stack, and one that creates the same large array from the heap. Call each of the subprograms a large number of times (at least 100,000) and output the time required by each. Explain the results.