Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 18 – Memory Management

Your name:

Your email address:

Your student ID number:

Once this form has been customized for your institution, you can use this button  to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience with

P1. Image Class

In this problem, you will define and implement a class that represents a digital, grayscale image. Grayscale images are stored in computers as arrays of data where each value within the array is an integer that ranges between 0 and 255. Zero represents black and 255 represents white. Because image processing algorithms often manipulate large amounts of data, speed is essential. Therefore, you will store the two-dimensional image data within a one dimensional array.


First, define the Image class as provided by the code below. Then, implement all of the functions for this class. Implement only those functions shown. You will be adding functions in later problems.


Note that the constructor takes the name of a file as a parameter. This file should be a text file of integer numbers separated by spaces. There are two images provided for you to test your code: image one is size 56 x 59 and image two is size 60x60. Take a look at the provided text files and corresponding bit-map images before trying to implement the constructor. The displayImage() function should display the stored image to the console window using the following rules: use a space for values less than 64, use a period if the value is greater than or equal to 64 but less than 128, use a * if the value is greater than or equal to 128 but less than 192, and use an @ otherwise.


Text Image (1)

Text Image (2)


Class Image




   Image(int nRows, int nCols, string filename);

   int getRows();

   int getCols();

   int getPixelValue(int row, int column);

   void displayImage();




   int* imArray;

   int numRows, numCols;



Submit all of the code for your answer in the space below.

R1. Categories of Memory

There are many types of memory available to C++ programs. In the space below, you will list the four categories of memory and the types of data each category stores. Be as specific as possible.

Enter a category of memory below.

Enter the type of data stored in the above category.

Enter a category of memory below.

Enter the type of data stored in the above category.

Enter a category of memory below.

Enter the type of data stored in the above category.

Enter a category of memory below.

Enter the type of data stored in the above category.

P2. Copy Constructor and Destructor

In this problem, you will be adding a copy constructor and destructor to the code you developed in problem P1. Specifically, you should write a copy constructor and destructor for your image class. The function definitions are provided below. Provide your implementations for the two functions in the space provided below.


Image(Image& ref)


Enter the code for the constructor in the space below.

Provide the code for the destructor in the space below.

R2. Common Memory Errors

Many kinds of memory errors can appear in programs written by novice programmers. This problem will explore several of these. In the following, sections of code will be provided. For each code segment below, indicate the type of memory error that will occur.

Code Segment 1


double salary;

double taxRate = 0.15;

double taxesDue;

taxesDue = salary*taxRate;



Code Segment 2


int badArray[200];

for (int i = 0; i <= 200; i++)

   badArray[i] = 0;



Code Segment 3


SomeClass* myFirstClass = new SomeClass(…);

myFirstClass = new SomeClass(…);



Code Segment 4


int* CreateIntegerArrayofSizeTen()


   int createdArray[10];

   return createdArray;


int* array = CreateIntegerArrayofSizeTen();



Code Segment 5


DerivedClass* derivedClass = new DerivedClass(…);

BaseClass baseClass = *derivedClass;

P3. Assignment Operator


Next, write an assignment operator for your Image class. Be careful: The size of the image on the left-hand-side of the operator is not necessarily the same size as the image on the right-hand-side. The function declaration is shown below. List your code in the space provided.


Image& Image::operator=(const Image& right);

R3. Constructors and Destructors

In the space provided, answer the following questions:

What are the two uses for single argument constructors?



When should you use a system-defined copy constructor?



What is the difference between destruction and deletion?



When should a destructor be explicitly called within a program?

P4. Testing the Image Class

Write a small program that tests the image-handling code written in problems P1-P3.

R4. Reference Counting

In the space provided, proceed through the process of reference counting for each of the three objects existing within the main function below. Include in your answer specific counts after each instruction. You should already be familiar with the operation and implementation of the SharedString class as supplied by the text.

int main()


   SharedString a;

   SharedString b;

   SharedString c;


   a = b;

   b = c = a;

   b = b;

   c = b;

   a = a;


   return 0;


Do not forget to send your answers when you are finished.