Assignment 5
Due 2/28/08 9:30 a.m.
(Must be done with your group)

  1. [synthesis] If a language wants to support garbage collection, the language must be designed appropriately.  In other words, one cannot design a language while being oblivious to garbage collection and hope that it will "just work" with garbage collection.  The language must be designed such that the garbage collector is able to: (a) find all the pointers in memory; and (b) identify which object each pointer points to.
    1. Explain the need for the two requirements given above
    2. Does C++ support these two requirements?  Support your answer either with quotes from the C++ language definition (available in the "interesting links" section of the class web page) or with examples and their output (much like how you use the PL Detective) from a C++ compiler/run-time system
  2. Consider the following program in C++:
    unsigned int *max(unsigned int a[5]) {
      unsigned int *maxptr = new unsigned int;
      for (int i = 0; i < 5; ++i) {
        if (*maxptr < a[i]) *maxptr = a[i];
      }
      return maxptr;
    }

    int main() {
      unsigned int a1[5] = {2,7,4,5,5};
      unsigned int a2[5] = {2,7,4,11,5};
      unsigned int *t;
      t = max(a1);
      printf("max of a1: %d\n", *t);
      t = max(a2);
      printf("max of a2: %d\n", *t);
    }
    1. [skill 8.1] Does the above program suffer from memory leak or dangling pointer bugs?  If so, indicate where and why.
    2. [skill 8.2] Rewrite the program above to eliminate the memory leak or dangling pointer bugs.
  3. Consider the following program in C++:


int *g;
// make the location pointed to by g contain the same value as p
void f(int *p) {
  g = p;
}

int main() {
  int *x = new int;
  *x = 5;
  f(x);
  delete (x);
  printf("g is: %d\n", *g);
}

  1. [skill 8.1] Does the above program suffer from memory leak or dangling pointer bugs?  If so, indicate where and why.
  2. [skill 8.2] Rewrite the program above to eliminate the memory leak or dangling pointer bugs.
  1. [skill 9.3] From the mystery grammar we see that all operators (+, >, AND) are left associative and have the same precedence.  Real languages, such as Java and C++ often put these operators at a different precedence. 
    1. For one of C, C++, or Java figure out the precedence and associativity of +, >, and AND.  You may accomplish this by writing programs in the language or by looking up a book or language definition for the language.  In your answer give either the programs that you used or quotes from the book/language definition that support your answer. 
    2. What are the implications of having the three operators at the same level in MYSTERY?  In other words, give an expression in your chosen language (i.e., C, C++, or Java) that would evaluate differently (or not evaluate at all) in MYSTERY.  Explain your answer.
  2. [skill 9.2] Page 339, Problem set, Q 9
  3. [Skill 7b.2] Give a concrete and realistic example that uses a union type.  By realistic I mean that the example should be motivated by a "real-world" situation and not be abstract.
  4. [skill 9.3] Calls are amongst the most complicated expression construct in programming languages.  There are many, many different ways of supporting calls and different languages often differ in their support for calls.  In this question, we will consider only the operand evaluation order issues with calls; later in the semester we will consider other issues.

Let's consider the following abstract call F(A1, ... An)F is the procedure being called.  While we are used to calling a procedures directly (e.g., print(...)), most languages, including MYSTERY allow complicated expressions for F.  For example, if a is an array of procedures, one can do a[2](5) in MYSTERY to call the procedure at a[2] with argument 5MYSTERY even allows a procedure to return a procedure.  Thus, if a procedure, f, returns a procedure, then one could write f()(x).  This calls the procedure returned by f with the argument x.  Needless to say, most languages allow one to pass arbitrarily complicated arguments (i.e., Ai). 

  1. Come up with and describe in detail a compelling example of why would one want to return a procedure from a procedure.
  2. Discover whether MYSTERY evaluates the procedure being called before it evaluates its arguments.  For example, with f()(x) does MYSTERY call the function f first or does it evaluate x first (i.e., looks up the value of x).  You may execute up to 4 PRINT statements.   
  3. Discover whether or not MYSTERY evaluates arguments to a call from left-to-right.  You may execute up to 4 PRINT statements.

Use this link to submit programs.  You should provide evidence that supports your argument.  You will lose 5% of the points for this question for each additional executed PRINT.