Lab 9: An Exercise on Exceptions

Due: 5/31 11:59pm

Please read the instructions carefully

This lab may be done solo, or in pairs.

Before you begin working on the lab, please decide if you will work solo or with a partner.

As stated in previous labs, there are a few requirements you must follow if you decide to work with a partner. I will re-iterate them here:

1. Goals

  • This lab will review some concepts like a templated container class (SimpleList).
  • This lab will cover implementing a class that supports Exceptions - your functionality of the class will throw an Exception to the caller under certain conditions.
  • Your template class implementation will support pointer types and should deallocate memory on the heap properly in order to avoid memory leaks.

2. Copying the code from my directory

Visit the following web link–you may want to use "right click" (or "control-click" on Mac) to bring up a window where you can open this in a new window or tab:

http://sites.cs.ucsb.edu/~emre/cs32/code/lab9/

You should see a listing of several C++ programs. We are going to copy those into your ~/cs32/lab7 github repo all at once with the following command:

cp ~emre/public_html/cs32/code/lab9/* ~/cs32/lab9

Note: If you get an error message, check the same instructions for previous labs.

After doing this command, if you cd into the /cs32/lab9 directory and use the ls command, you should see several files–the same ones that you see if you visit the link above.

If you don't see those files, go back through the instructions and make sure you didn't miss a step. If you still have trouble, ask your TA or mentor for assistance.

3. Implementing the SimpleList.cpp functionality.

  • SimpleList.h is given in the starter code, and your task is to provide the proper implementation for the SimpleList class.

A few notes about this specific implementation (as described in SimpleList.h):

  • This class will use templates and a SimpleList can store any type.
  • Your container will be an array (elements) of a templated type.
    • This array will be allocated on the heap.
  • You may assume that your list capacity is 10 elements (defined as CAPACITY).
  • You will need to keep track of the current number of items in your SimpleList using the numElements variable.

A few notes about throwing Exceptions:

Your implementation will need to implement the following functions, and throw Exceptions in certain cases:

SimpleList();
SimpleList constructor that sets numElements to 0 (i.e. the SimpleList is constructed with 0 elements). Also allocates the array elements with a size of CAPACITY.
~SimpleList()
SimpleList destructor must delete the dynamically allocated array on the heap as well as deleting any elements in SimpleList that exist on the heap.
T at(int index) const
Returns the element at index location. Throws a InvalidIndexException if there is no element at index.
bool empty() const
Returns true if the list is empty, false otherwise.
T first() const
Returns the element at index 0. Throws an EmptyListException if the list is empty.
T last() const
Returns the last element in the SimpleList. Throws an EmptyListException if the list is empty.
int getNumElements() const
Returns the number of items currently stored in the SimpleList.
void insert(T item)
Inserts item at the first possible slot towards the end of the list. Throws a FullListException if the list is at Capacity.
void remove(int index)
Removes the element at index. You will need to shift all elements inserted after the index position down slot in the array in order to prevent any "holes" in the SimpleList.
  • First checks if the SimpleList is empty - throws an EmptyListException if the SimpleList is empty.
  • Throws an InvalidIndexException if there is no element at the index position.

Another note on checking if a template type is a pointer:

  • You may find the following std functionality handy if you want to determine if a Template type is a pointer or not:
    • std::is_pointer<T>::value in <type_traits>.
    • This will return true if T is a pointer type, and false otherwise.
  • You may also encounter a compilation issue when attempting to delete a Template pointer type T (and not T*).
    • C++ is kinda finicky about this, but there are ways to destruct elements of Templated pointer types.
    • C++ isn't happy when trying calling delete on a template type that is a pointer, but is not explicitly declared as a pointer in the parameter argument (even if it passes the is_pointer<T>::value check).
    • You can get around it by overloading functions declaring T specifically as a pointer type (or not). For example, here is a simple proof-of-concept illustrating this point:
template<class T>
void destroy(T element) {
    // do nothing
}

template <class T>
void destroy(T* element) {
    // delete the pointer type
    delete element;
}

template <class T>
void function1(vector<T> x) {
    if (is_pointer<T>::value) {
        //delete x[0]; // may not compile even if T is a pointer type

        // Will call destroy(T* element)
        destroy(x[0]);
    } else {
        // will call destroy(T element)
        destroy(x[0]);
    }
}

int main() {
    vector<int*> x;
    int* y = new int;
    x.push_back(y);
    function1(x);
    return 0;
}

4. Testing

The following executables will be generated by typing make all:

  • testSimpleList1
  • testSimpleList2
  • testSimpleList3

You can test each executable separately by executing each individual binary file or you can run all tests by typing make tests.

There will also be a memory test for testSimpleList3 that will check for memory leaks. This will use testSimpleList3 to see if your code is deallocating memory properly. You can run this memory test after testSimpleList3 is compiled with:

make lt01

5. Submitting via Gradescope

You will turn in SimpleList.cpp for this lab.

The lab assignment "Lab 9" should appear in your Gradescope dashboard in CMPSC 32. If you haven't submitted anything for this assignment yet, Gradescope will prompt you to upload your files.

For this lab, you will need to upload your modified file (i.e. SimpleList.cpp). For this lab, you are required to submit your files with your github repo.

If you already submitted something on Gradescope, it will take you to their "Autograder Results" page. There is a "Resubmit" button on the bottom right that will allow you to update the files for your submission.

For this lab, if everything is correct, you'll see a successful submission passing all of the autograder tests.

Remember to add your partner to Groups Members for this submission on Gradescope if applicable. At this point, if you worked in a pair, it is a good idea for both partners to log into Gradescope and check if you can see the uploaded files for Lab 9.

Author: Mehmet Emre

Created:

The material for this class is based on Prof. Richert Wang's material for CS 32