For this assignment, you will implement a library that provides safe pointer operations. Smart pointers provide additional functionality on C and C++ pointers by wrapping the operations performed on a pointer, such as copying, dereference, and destruction. Smart pointers have been provided for some time in the C++ standard library (auto_ptr) and in the Boost library (scoped_ptr and shared_ptr) to enable safer dynamic memory management. For now, your smart pointers will not provide any assistance for dynamic memory management, but they will be designed to prevent null-pointer and out-of-bounds errors.
You will impement two classes: ptr and array_ptr. For now, your classes will only store int pointers. For both of these classes, you will need to implement a constructor, a copy constructor, an assignment operator (operator=), an equality operator (operator==), and an inequality operator (operator!=). For the ptr class, you will need to implement a dereference operator (operator*) that performs a safety check to ensure that the pointer is not null. For the array_ptr class, you will also need to implement an index operator (operator[]) for array-indexing. The index operator should check for both null pointers and ensure that the index being accessed is within the bounds of the array. Finally, you will need to implement friend functions that wrap calls to new and delete that properly initialize the internal structure of ptr and array_ptr. You can call these functions new_ptr, new_array_ptr, delete_ptr, and delete_array_ptr. You should set the smart pointer's internal pointer to null to provide some prevention for use-after-free and double-free errors.
I have provided a brief version of the ptr class for you to start with. The code can be found in the file ptr.hpp. We have also provided a test driver and a few test programs for your smart pointer classes. You should add any additional tests that are necessary to test the functionality of your smart pointer classes to driver.cpp. The test programs test_null.cpp, test_bounds.cpp, and test_delete.cpp should fail with an assertion when you attempt to execute them because they contain memory safety errors that your smart pointers should detect.
Below is a checklist of the functions and features that you must implement for this homework assignment:
The lists above may look a bit daunting, but the code for this assignment should not be too complex. Each of the header files in my solution is less than 75 lines long.
Pointer arithmetic allows us to adjust the address pointed to by a pointer. For example, if we have an int pointer that points to 0x10000000 and increment the pointer once, it will then point to address 0x10000004. The following code example demonstrates pointer arithmetic further.
#includeint main(){ int *p1 = new int[3]; int *p2 = p1; assert(p1[0] == *p2); p2++; assert(p1[1] == *p2); assert(p1[1] == p2[0]); assert(p1[2] == p2[1]); p2++; assert(p1[2] == *p2); return 0; }
If you run this code, none of the asserts will fail.
Download the challenge code sample (challenge.cpp) from the github repository. Find the comment in the code and print the value requested by the comment. The only code you should need to add is code that performs pointer arithmetic (or indexing) and prints the value using std::cout. You should not add any new function parameters or variables.
Submit the file challenge.cpp with the added statement that prints the correct value for extra credit.
Please submit your solutions via Canvas as a zip or tar file.