CSE 333 Exercise 14
Due: Wednesday, February 18 by 11 AM
Rating: 1 (note)
Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".
This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.
Goals
- Learn how to use C++ smart pointers to handle heap memory resource management.
Problem Description
Consider the following C++ file, named ex14.cc: (to download: right-click the link, then left-click "Save link as...")
/*
* Copyright ©2026 Justin Hsia and Amber Hu. All rights reserved.
* Permission is hereby granted to students registered for University of
* Washington CSE 333 for use solely during Winter Quarter 2026 for
* purposes of the course. No other use, copying, distribution, or
* modification is permitted without prior written consent. Copyrights
* for third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the
* authors.
*/
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
int main(int argc, char** argv) {
// Allocate an integer on the heap, initialize to value 5.
int* x = new int(5);
std::cout << "*x is: " << *x << std::endl;
// Allocate a vector of integers on the heap, add some values to
// that vector, sort the vector, print the values.
std::vector<int>* v = new std::vector<int>;
v->push_back(13);
v->push_back(42);
v->push_back(17);
std::sort(v->begin(), v->end());
std::cout << "sorted v: ";
for (int& el : *v) {
std::cout << el << " ";
}
std::cout << std::endl;
// Allocate a vector of (integer pointers) on the stack, add some
// values to the vector from the heap, print the values.
std::vector<int*> v2;
v2.push_back(new int(13));
v2.push_back(new int(42));
v2.push_back(new int(17));
std::cout << "unsorted v2: ";
for (int* el : v2) {
std::cout << *el << " ";
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
If you try compiling and running this program under valgrind,
you'll notice that there are several memory leaks in it, all
arising from allocating values on the heap using new
but not deallocating them.
Your challenge is to fix these memory leaks.
However, to make it interesting, you are not allowed to directly
use delete (or free, which would be a
mistake, of course).
Instead, you have to modify the code to use the
std::unique_ptr smart pointer.
So, for example, line 19 of the program allocates an integer on the
heap using new.
You will need to modify that line to transfer ownership of the
allocated integer to a std::unique_ptr<int>.
Along with the modifications you make, please change the comment at the top of the file to include your name and CSE or UW email address.
Implementation Notes
Coding Guidelines
The only modifications you are allowed to make to the code are
those involved with adding in the appropriate
std::unique_ptr support.
Scope of Your Changes
Remember that this excercise has a rating of 1! This is because the changes to the code shouldn't be too complex (i.e., the support you add for smart pointers should not change the program's behavior).
Style Focus
Smart Pointer Types
We discussed multiple kinds of smart pointers in lecture, and they all have different use cases and behaviors, so think carefully about which type you want to use. In particular, think about what kind of ownership makes the most sense for your changes, as you should use the "best-fit" even if another type might also work.
Submission
Submit the following file(s) by creating an ex14-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:
ex14/ex14.cc
Your code must:
- Compile without errors or warnings on CSE Linux machines
(lab workstations,
attu, or CSE home VM). - Have no runtime errors, memory leaks, or memory errors
(
g++andvalgrind). - Be contained in the file listed above that compiles with the
command:
$ g++ -Wall -g -std=c++17 -o ex14 ex14.cc
- Have a comment at the top of your
.ccand.hfiles with your name(s) and CSE or UW email address(es). - Be pretty: the formatting, modularization, variable and
function names, commenting, and so on should be consistent with
class style guidelines.
Additionally, the linter shouldn't have any complaints about your
code (
cpplint.py). - Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.