CIS 343

Scope

Winter 2023

Scope

  1. Describe/write a simple program that demonstrates the usefulness of a static variable. This should be a "real" program not a "toy" program.
  2. 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:

     

    1. Assuming static scoping, in the following, which declaration of x is the correct one for a reference to x?
      • sub1
      • sub2
      • sub3
    2. Repeat part a, but assume dynamic scoping.

Lifetime

  1. 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);
    }
    
  2. When are each of the variables above deallocated?
  3. 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

W3c Validation