Goals
- Reinforcing some fundamental aspects of C++
- Dynamic Memory
- Destructors (RAII)
- Exceptions
- References
- null terminated strings
- How things are laid out in memory
- Header-guards & Makefiles (cont.)
- Being on the shorter side, so that you can get started on the next HW assignment (HW03) which is slightly bigger than past assignments.
Collaboration
For assignments in CIT 5950, you will complete each of them on your own. However, you may discuss high-level ideas with other students, but any viewing, sharing, copying, or dictating of code is forbidden. If you are worried about whether something violates academic integrity, please post on Ed or contact the instructor(s).
Setup
Task 1. Setup your docker to gain access to a development environment for the course.
If you haven’t already, you need to follow the Docker Setup. We recommend you try and figure this out ASAP.
You need to do this assignment in the docker environment setup by the course. You should create a different directory in your docker environment to run your code, but you do not have to.
Once you are in docker, you must run the following commands to download the starter files for the project.
curl -o cpp_string.zip https://www.seas.upenn.edu/~cit5950/current/projects/code/cpp_string.zip
and
unzip cpp_string.zip
after this you can ls
again and see that we have the file simple_string.cpp
, Makefile
, simple_string.hpp
, test_suite.cpp
, test_string.cpp
, catch.hpp
and catch.cpp
added to our local directory.
From here you can start working the assignment by opening the files we just created with vim
, VSCode or another editor if you have one you prefer.
Instructions
Once you have followed the setup instructions, you should have a folder that contains the files for this assignment.
Note: you are restricted from using most of the standard C++ library in this assignment. See the section below on allowed functions for more.
Required Knowledge
This homework has you writing C++ code and what you need has been covered in lecture already. However, there will be some activities in lecture where we draw memory diagrams similar to what you will do for the second part of this assignment.
simple_string
For this assignment, you will be porting your simple_string
code form HW00 into a more proper C++ object.
Our C++ object will also have a few more member functions to give you practice with more complex operations and using a vector / string. These two member functions are first_find_of
and split
.
If you are stuck on anything related to C++ objects
we encourage you to look at the relevant lecture materials.
We suggest you start by opening simple_string.hpp
and reading the code and comments.
Once you have familiarized yourself with what is going on, you should open the (mostly) empty simple_string.cpp
and provid an implementation for every function described in the simple_string.hpp
header files. Your cpp
file should #include
the corresponding hpp
file.
Allowed / suggested functions & headers
For this assingment you are allowed to write any helper functions you need, but you are restricted to using the following headers and the following functions. You do not need to use all of these, do what you think would be best for your code. If you do not see a function listed that you think should be ok to use, please ask and we can allow it or disallow it.
- cstddef
size_t
- vector
std::vector
- stdexcept
std::out_of_range
- iostream
cout
cerr
endl
Compilation
Header Files and Header Guards
For this assignment we gave you simple_string.hpp
, which is a header file.
Header files contain the declarations of types and declarations of functions that are defined in the corresponding cpp
file.
For your code to get full credit, you will need to add header guards like this to your simple_string.hpp
, similar to what you did in the first simple_string
assignment.
If you have coded in C or C++ before you may be tempted to use #pragma once
instead of header guards. While this is relatively common, it is not actually standard in C++ and can have varying details in different C++ compiler (or may not be supported by a compiler at all!). Thus we are not going to use #pragma once
in this class)
Note that your submission will be partially evaluated on the number of compiler warnings. You should eliminate ALL compiler warnings in your code.
Makefile
You must also modify the provided Makefile
to compile the code into a runnable test_suite.o
.
We provide most of the makefile for you, and you will also note that the makefile includes steps for building catch.o
and test_suite.o
and then using it to compile your test_suite
program.
Like the last assignment, you will have to build off of what we did in the makefile. This time you will need to make it build test_string.o
and simple_string.o
from the corresponding cpp
and hpp
files and then use those to build the test_suite.
This is not as hard as it may sound! It may look scary, but you can do it!
Makefiles should have already been covered in a past class, but in-case you are stuck on it, we included a crash course in the previous assignment.
Testing your code
Valgrind
You need to test your submission on whether there are any memory errors or memory leaks. We will be using valgrind to do this. To do this, you should try running:
valgrind --leak-check=full ./test_suite
If everything is correct, you should see the following towards the bottom of the output:
==1620== All heap blocks were freed -- no leaks are possible
==1620==
==1620== For counts of detected and suppressed errors, rerun with: -v
==1620== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
If you do not see something similar to the above in your output, valgrind will have printed out details about where the errors and memory leaks occurred. We went over how to read valgrind errors some time in recitation or lecture in the first or second week.
Note that the last part of the valgrind command is just what we would input to run our program normally. You should try running your program on various inputs. (e.g. some valid and some invalid) to make sure it passes for all test cases.
Autograder
This assignment has an autograder to test the functionality of your simple_string
object, but this mostly just runs the tests we provide. When you submit to gradescope (see below) it should run tests for you, but you are HIGHLY ENCOURAGED to run the test locally. Infact, if the code fails on gradescope in the test_suite
it will not tell you much other than it failed. It will be up to you to debug it locally.
Submission
Please submit your completed Makefile
, simple_string.cpp
and simple_string.hpp
to Gradescope
:wq