CS 509, Advanced Programming II

Fall 2001 - Test 5


  1. Explain the advantage of using the constructor initialization list instead of initializing member variables in the constructor body.


    Putting an initialization in a constructor's initialization list prevents the default initialization that would occur if the initialization occurs in the constructor's body. The advantage is doing less work by avoiding a useless initial initialization.

    Many people claimed that an initializer list performs compile-time initialization, but all non-static member variable initialization must be done at run-time. (Why?) A couple of people pointed out that the initializer list has to be used to initialize member constant values, which is true, but not quite the answer I was looking for because it's not possible to initialize member constants in a constructor body.


  2. Explain how it is possible to define a local array variable and not specify a size for the array.


    An array's size may be skipped if the array declaration includes an initializer list. The compiler can determine the size of the array by counting the number of elements in the list.

    Most people got this right (some even noted that I provided the answer for them in question 4). Most of the people who got this wrong tried to base their answer on dynamic storage allocation, which requires a size.


  3. The following is a specification for constant-folding an expression:

    The following code is supposed to implement constant-folding for equations as given by the specification:

    bool expression::constant_fold(void) {
    
      if ((node_t == number) || (node_t == var))
        return false;
    
      bool b = lhs->constant_fold() || rhs->constant_fold();
    
      if ((lhs->node_t == number) && (rhs->node_t == number)) {
        // Combine the constants using the operator.
        b = true;
        }
    
      return b
      }
    

    Does the code implement the specification? Explain.


    The code does not implement the specification. The problem arises from the logical or operator ||. Logical or is a short-circuiting operator, that is, when evaluating a || b, a is evaluated first and, if true, b is not evaluated (because the or of a true value with anything is true).

    This short-circuit behavior causes problems in expression::constant_fold() when lhs->constant_fold() returns true, because then rhs->constant_fold() is never called, which is contrary to the specification that both sides of the expression be folded.

    This question is based, of course, on an error I made while coding up the fourth assignment.


  4. Given the declaration

      static1 const2 char3 *4 const5 letters6[]7 = {"A", "B", "C"}
    

    explain what each of the seven items means. Assume the declaration occurs at file scope.


    The easiest way to solve this problem is to read from right to left:

    letters6 is an array ([]7) of constant (const5) pointers (*4) to character (char3) constants (const2) that is a global but restircted to file scope (static1).

    Almost everybody tripped up on the meaning of static, claiming that it meant that letters was initialized only once. letters is initialized only once, not because it's declared static, but because it's a global variable. The only-once meaning of static occurs when it's used with procedure-local (also known as automatic) variables.



This page last modified on 6 December 2001.