Lab Assignment #4 Solution



This lab explores two important aspects of dynamic memory management. The first part deals with “The Big 3,” the destructor, copy constructor and assignment operator that need to be overloaded for any class that holds dynamic memory. The second part deals with the bad_alloc exception and how it can be handled.

Begin by making a separate directory for this assignment. In that directory you will be using the main that I have given you and creating a file that will have a class, both the class declaration and the function implementations. (For this lab function implementations can be done under the class declaration, in the same file.)

The class, Numbers, that you declare will have these private variables:

unsigned long * data;

std::size_t used;

std::size_t capactity;

The functions should be:

  1. A default constructor that sets up an initial array of 5 unsigned longs, sets capacity to 5 and used to 0;

  2. An add function that receives an unsigned long as a parameter and puts it into the next available spot.

  3. A resize function that is only called when the add function checks for and discovers that used == capacity. It should increase the size of the array by five.

  4. a remove_last function that takes out the last thing added to the array. (All you need to do for this is to decrement the used counter. You do not need to change the capacity.)

  5. A display function that prints out all the numbers in the array, separated by spaces.

Declare these functions in the class declaration and implement them immediately below that. (You do not have to use two files.)

In the main, notice that I have #include “numbers.h” at the top, declare two objects of your Numbers class, N1 and N2.

Into N1 added the numbers 2 4 6 8 10 12 14. (If things crash there’s a problem with your resize function.)

Called N1.display(); to confirm that your numbers are in there.

Assign N2 to be a copy of N1. N2 = N1;

Call N2.remove_last( ); four times.

Into N2 add the numbers 5 10 15.

Call N1.display( ); // notice that this is the first object
// not the one you just put numbers in

On your Answer sheet write the answers to the following:

  1. What do you see?

  2. Is this a problem and why?

  3. What caused this to happen?

Now reopen the file where you defined your class, and add to your class an overloaded assignment operator. Remember that an assignment operator has three parts: 1) check for self-assignment, 2) delete the existing array and 3) create a new array the same size as the one you’re copying from, copy the values for used, capacity and then copy all the data from the other array into this new one. (The new one will be held by the main pointer of the object of which this operator is a member.)

Compile and run the program again without changing anything in the main.

On your Answer sheet write the answers to the following:

  1. What do you see?

  2. Is it different from what you saw before?

  3. What caused this to happen – why is it different?

Part Two:

Now, in the main, uncomment this for part two.

unsigned long item = 0;


while (true) // yes, this is an infinite loop

{ Numbers N3;

for (int i = 0; i < 10000; i++){





catch (bad_alloc){

Cout<<”Memory failure after adding “<<item <<endl;


On your Answer sheet:

  1. What number did you reach before crashing? From this number can you deduce how many bytes were allocated? (An unsigned long takes 4 bytes.)

  2. Why did you run out memory, since we were making a series of 100 item arrays?

  3. What can you do to your class to fix this?

  4. After you have tried this fix do you still get the same number? Or an infinite loop?

Please submit you source code and either submit or turn in your answer sheet, according to the instructions of your lab instructor.