Midterm Exam, CSCI 330, Fall 2017


  1. [10 points] Draw a diagram that represents the compilation of a multi-file program. Label the parts. Draw a rectangle around all processes and an oval around all files. Please include the following items (items may be used multiple times):
    compilerexecutable linker object code
    preprocessor source code system header file system library
    temporary source codeuser header file

  2. Namespaces
    1. [3 points] Give the code required to declare the function prototype: void PrintWonder(WonderT w); in a namespace called GameDebugging.
             namespace GameDebugging{
                  void PrintWonder(WonderT w);
             }
             
      You were only asked for the prototype, not the function implementation. I did not take points off if you implemented the function but this was a waste of time.

    2. [2 points] Give the code required to call the function defined in the previous question. Assume that no using statement has been executed.

      GameDebugging::PrintWonder(w);

    3. [5 points] Why should using namespace std; be avoided in header files?
      Including using namespace std in a header file means that when the file is included into a source code file the namespace is included for that source code as well. This leads to namespace pollution which means that all of the standard identifiers have now been added to the global namespace for that file.

      This is will cause problems if the programmer has declared identifiers which are the same as the identifiers in the standard namespace. In this case, the code, which compiled before the header file was included, will no longer compile.

  3. [5 points] Compare and contrast a standard array in C and a C++ STL array class array.

    Both the C style array and the STL array class provide a container which can store a known number of an arbitrary type. The size of the container must be known at compile time. Both provide constant time access to an element through the index ([]) operator. Both provide unsafe indexing.

    There are a number of differences between these two array types. The C style array can only be passed by reference, while the STL array can be passed by reference or by value. The assignment operator in the C style array is a shallow copy, while a deep copy is performed in the STL array. There is no safe indexing function in the C style array while the STL array has the at function which will perform bounds checking and throw an exception if the index is out of bounds. The STL array has a number of additional functions such as front, back, and iterators which the C style array is missing.

    Compare and contrast means you
    • State how the items are the same
    • State how the items are different.
    You need to to both. I didn't take points off this time, but I will on the final if you fail to do both.

    It seems to me that you really should mention the deep copy provided by the = operator and the ability to pass by value.

    Mentioning that "The STL array provides many functions." without any supporting information is probably insufficient. You really should mention at least at, and =.

  4. References
    1. [2 points] What happens when a reference variable is assigned the first time?
      The reference variable is treated as a pointer and the address of the item on the right hand side is copied into the reference variable.

    2. [2 points] What happens on subsequent assignments to a reference variable?
      The variable is treated as a "normal" variable in that the pointer is automatically dereferenced. The value of the referenced variable is changed.

    3. [6 points] Draw a diagram that represents the memory and contents of memory after the following code is executed.
      int x = 5;
      int & y = x;
      y = 7;
      

      It seems to me that you should have some differentiating between x and y. If both are arrows to the same memory there is no distinction between the two.

  5. [5 points] A class which allocates dynamic memory should provide a copy constructor. What problems can occur is a copy constructor is not supplied?

    If no copy constructor is provided, any pass by value will result in a shallow copy. This means that both the parameter and the argument will share the dynamic memory.

    The first problem encountered that it is not truly a pass by value for the dynamic portion of the class. Changes to the "value" parameter will be reflected in the argument, which is unexpected behavior.

    The second problem is that if the class has a destructor which properly frees memory, the memory for the parameter, and thus for the argument will be destroyed. This results in the argument having a "dangling" pointer, or a pointer that points to memory which has been deallocated.

    I'm not sure I can come up with a scenario where a memory leak will occur. I think many of you are still confusing the copy constructor with an overloaded assignment operator.

    I probably did not grade this question carefully enough. I should have taken off points if you did not mention the two problems I described above.

  6. Given the following code
    int x = 0;
    try {
        x++;
        AFunctionCall();
        x++;
    } 
    catch (int y) {
        x+=y
    }
    
    Assume that no other catch statements are executed and that somewhere in the call sequence starting at AFunctionCall the statement throw 10; is executed.
    1. [6 points] Describe the flow of control for the above code.
      	 x is assigned a value of 0
      	 The try block is entered
      	 x is incremented
      	 The function is called
      	 The throw occurs
      	 The catch block is executed with y = 10
      	 x is set  to 11
      	 

    2. [2 points] What is the value of x when the above code is executed?
      11

    3. [2 points] What would be the result if the call sequence executed the statement throw 1.0; instead of throw 10;?
      Since no other catches have been declared, the program will exit.