Scope
- Describe/write a simple program that demonstrates the usefulness of a static variable. This should be a "real"
program not a "toy" program.
- Consider the code below:
// The main program
var x;
function sub1() {
var x;
function sub2() {
. . .
}
}
function sub3() {
. . .
}
Assume that the execution of this program is in the following unit order:
- main calls sub1
- sub1 calls sub2
- sub2 calls sub3
- Assuming static scoping, in the following, which declaration of
x
is the correct one for a reference to x
?
- Repeat part a, but assume dynamic scoping.
Lifetime
- For each of the variables
a
- d
, is the variable allocated statically or dynamically?
Where is the variable stored? (Stack, heap, or global)
int a;
int main() {
static int b;
int c;
int *d;
d = malloc(sizeof(int)*3);
}
- When are each of the variables above deallocated?
- Dynamic type binding is closely related to implicit heap-dynamic variables. Explain this relationship.
(Questions in this exercise come from both Prof. Bowman and the textbook.)
Updated Monday, 3 April 2023, 9:46 AM