Assignment 5, Pointer types
Due 02/24/04 9:30 a.m.
(Must be done with your group)

  1. 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. Languages differ greatly in how they handle arrays.  In this question we will focus on one issue with arrays: what happens when one array is assigned to another (assuming the two arrays are of compatible types).  Consider, for example, two variables declared as follows in MYSTERY syntax:

    VAR a: ARRAY[1 TO 5] OF INTEGER;
    VAR b: ARRAY[1 TO 5] OF INTEGER;
    BEGIN ... END;

    What happens when we have an assignment a := b?   If the assignment is "by reference" (which is how Java works), variables of array type are represented as pointers and the outcome of the assignment is that a ends up pointing to the same array that b points to (i.e., alias).  If the assignment is "by value" (which is how Modula-3 works), then the assignment copies the contents of array b to array a

    1. Use this link to figure out if MYSTERY uses array assignment by reference or by value.  Your answer to this question should describe which one MYSTERY uses and also provide the evidence that enabled you to come to your conclusion.  You submissions may execute up to a total of 6 PRINT statements.  Additional attempts will be charged at 5% of this question's points per executed PRINT.
    2. Discuss the relative advantages and disadvantages of array assignment by value and array assignment by reference.  Refer to the "Characteristics" (i.e., first column) in Table 1.1 of Sebesta in your arguments.
  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 the three operators in MYSTERY.  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.
  4. Problem set p. 315, #4
  5. 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 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.