This assignment will give you experience with the "big 3" for a new string class. If a program does a lot of text manipulation, it can be helpful if it uses a string class that does basic, automated memory management. For example, if two string variables are assigned to each other, it would be useful to have them point to the same underlying buffer (to avoid duplication), but to reassign the variable if the string is modified. You are going to create a string library that implements this.
In particular, you will implement a class cow_string. In this case
"cow" stands for "copy on write", most definitely note an implied
reference to livestock frequently found in Wisconsin. The class will store
arrays of characters and be implemented using the C-style string operations
like strcpy. To avoid duplication, cow_string objects point to
shareable_string objects which contain both a string and a count of the
number of cow_string objects that share that string.
More specifically, download the following code and get it to run with no errors:
Note the main.cpp contains code to implement a simple editor. This editor responds to the following commands:
a: Append a line to the end of the buffer a given number of timese: Edit line n at index i to character cr: Replace line n with new textd: Delete line np: Print the buffer starting at line n
The code in main is designed using the stream-of-consciousness model (that is,
not well structured). This is meant to illustrate why writing code using this
model is not recommended.Change only cow_string.cpp. Talk to your instructor if you believe
you need to change other files.
Do not add additional includes to cow_string.cpp. In particular, do not
use std::string. Use the C-style string (strcpy, strlen, etc.)
to process strings.
The original code does not compile because code needs to be added to specify
the argument to the generic_string constructor. Adding this code is
part of the exercise.
When you are finished, submit just cow_string.cpp to esubmit
as 5cow. Your solution will only be graded for correctness, not
style issues. You may discuss your solution with others, but each
student is to submit their own.
If you are getting linker errors when building in CLion, edit
CMakeLists.txt and make sure both generic_string.cpp and
cow_string.cpp (but not the .h files) are listed between the parens
for add_executable. For example, your file should look similar to
CMakeLists.txt, though you may have a different
assignment name or other differences.
If you get a message about there being bytes that did not get released,
double-check your logic for updating the underlying_string->count and
deleting the underlying_string object. The deletion should happen
whenever the count reaches zero.
If you are getting an error message about double deletes, check that your
code maintains the underlying string counter correctly. That counter
should be a count of the number of cow_string objects that refer to a
given underlying string object, so the count should be two after
executing the code
cow_string a = "hello";
cow_string b = a;