Project 0: Memory Management and Using GL

Due: Friday, Sept. 13, before 9:00 pm

There are no late submission folders for Project 0. You must turn it in on time to receive credit.


Addenda


Objectives


Introduction

In this project, you will complete a C++ class by writing a copy constructor, destructor, and assginment operator. Furthermore, you will use Valgrind to check that your program is free of memory leaks. Finally, you will submit your project files on GL. If you have submitted programs on GL using shared directories (instead of the submit command), then the submission steps should be familiar.


Assignment

Step 1: Create your working directory

Create a directory in your GL account to contain your project files. For example, cs341/proj0.

Step 2: Copy the project files

Change to your working directory (using the cd command) and copy the project files with the following command:

cp /afs/umbc.edu/users/c/m/cmarron/pub/www/cs341.f19/projects/proj0files/*.* .

Step 3: Complete the my_array class

The my_array class implements a simple, dynamically-allocated array with bounds checking on the element access operator. Complete the class by implementing the copy constructor, destructor, and assignment operator for the my_array class. You will need to add function prototypes, given below, to the header (.h) file and implement the functions in the .cpp file.

// Copy constructor my_array( const my_array& arr ); // Destructor ~my_array(); // Assignment operator my_array& operator=( const my_array& arr );

Step 4: Test your program

There are two test programs provided, test0.cpp and test1.cpp. However, test1.cpp is incomplete, and you must finish writing the test code for the assignment operator.

Be sure that both test programs compile with your completed my_array class and that they produce correct output.

Note: it is not sufficient for the test programs to compile and run. Both programs will compile and run even if you do no work on the project, but test1.cpp will give incorrect results. You must check that your output is correct.

Step 5: Check for memory leaks

Run the test programs using Valgrind. For example, assuming you have compiled test1.cpp, producting the executable test1, run the command

valgrind test1

If there are no memory leaks, the end of the output should be similar to the following:

==8613== ==8613== HEAP SUMMARY: ==8613== in use at exit: 0 bytes in 0 blocks ==8613== total heap usage: 6 allocs, 6 frees, 74,048 bytes allocated ==8613== ==8613== All heap blocks were freed -- no leaks are possible ==8613== ==8613== For lists of detected and suppressed errors, rerun with: -s ==8613== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

The important parts are “in use at exit: 0 bytes” and “no leaks are possible.” The last line is also important as memory errors can lead to leaks.

Step 6: Link your shared directory

Do not complete Steps 6 and 7 until directed to do so by your instructor. The submission directories will not be created until after the last day to add a class (Sept. 11).

Follow the instructions on the Project Submission page to make a symbolic link to the shared directory in your home directory.

Step 7: Submit your files

See the “What to Submit” section, below.


Implementation Notes

Each project has a section on implementation notes. These point out some issues that you might encounter while developing your code. You should look through the Implementation Notes before you start coding.

For Project 0, there are only a few notes:


What to Submit

You must submit the following files to the proj0 submit directory:

If you followed the instructions in the Project Submission page to set up your directories, you can submit your code using the following command:

cp my_array.h my_array.cpp test0.cpp test1.cpp ~/cs341proj/proj0/