CS 509, Advanced Programming II

Fall 2003 - Test 5


  1. According to Koenig and Moo, commercial string library implementations often take the more tedious route of defining specific versions of the concatenation operator for every combination of operands. Give an example of what it is that commercial string libraries do, and explain why it is that they do this.


    See the text, the middle of page 220.

    Most answers weren't even close to being right, although some were plausible if you forget what the question was supposed to be.


  2. Give an example of the virtual flip flop; that is, give some classes and code to show what the virtual flip-flop is.


    The virtual flip-flop occurs because while either the dynamic or static type of a instance reference or pointer determines the kind of member-function look-up used, only the static type determines which form of look-up is used.

    As an example, given

    struct Parent
      void munge() { ... }
    
    struct Child : Parent
      virtual void munge() { ... }
    
    int main()
      Parent * pp = new Child
      pp->munge()
    

    main() calls Parent::munge because the static type of pp is Parent, and in Parent munge() is non-virtual.

    Many answers to this questions had examples that didn't contain the virtual keyword; as you might expect, those answers weren't successful. A few answers used virtual class, which isn't even correct syntax.


  3. A colleague of yours, reviewing Koenig and Moo's implementation of a reference-counting handle, decides that it would be simpler and more efficient to store the reference count itself in each handle, rather than a pointer to the reference count as Koenig and Moo do. In other words, your colleague wants to replace

    template < T >
    class Handle {
    
      public:
    
        // blah blah blah
    
      private:
    
        T * ptr;
        size_t * ref_cnt;
      };
    

    with

    template < T >
    class Handle {
    
      public:
    
        // blah blah blah
    
      private:
    
        T * ptr;
        size_t ref_cnt;
      };
    

    What do you think of your colleague's scheme?


    Your colleague's scheme is disastrous. The point behind Koenig and Moo's scheme is that the reference count for an object instance being handled can be simply managed in one place but yet still be available to all handles referencing the object.

    pointers to reference counts

    By moving the reference count into each handle, your colleague has made it much more difficult and expensive to maintain the reference count; now each handle has to be tracked down so it's reference count can be changed. In fact, without extra bookkeeping information, it's impossible to maintain the reference count.

    reference counts

    Most answers that weren't wrong managed to dither around not being completely wrong, but few answers indicated exactly what is the problem with eliminating pointers to the reference count.


  4. Give a test case that demonstrates that a solution to the fifth assignment can verify true statements made in a report.


    The example from the assignment does the job:

    1: Computer 2 is flaky.
    2: Computer 1 is faulty.
    1: Computer 2 is faulty.
    

    See the assignment page for an explanation.

    Not suprisingly, most answers to this problem were correct.



This page last modified on 12 December 2003.