$29.99
Assignment 3: How Smart is your Pointer?
Smart Pointer Implementation
Quoting Wikipedia: In computerscience,asmartpointerisanabstractdatatypethatsimulates a pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bugs caused by the misuse of pointers, while retaining efficiency. Smart pointers typically keep track of the memory they point to, and may also be used to manage other resources, such as network connections and file handles. Smart pointers originated in the programming language C++.
In this last assignment, we will try to tackle the implementation of a “smart pointer” in its simplest form.
Please note that for all the questions, you are responsible of providing the appropriate test code in your main function. The output of your program should reflect clearly the answers for each question. Failing to do so will be penalized.
Also note that you need todoublecheckandconfirmthatyourcodecompilesbefore submittingtomyCourses.Thebestwaytominimizecompilationissuesonourside,is to provide only one cpp file containing all the code.
uestion 1 (15 pts)
Research smart pointers provided by the standard library (since C++11). List them and explain the difference between them.
uestion 2 (15 pts)
Design and implement your own smart pointer class called SmartPointer that will automatically delete the memory for built-in integer type that is allocated through it.
SmartPointer class should be used this way in your main() function:
SmartPointer sPointer(11);
2
Here we are passing 11 as an argument to the constructor of SmartPointer class. The constructor will allocate an integer variable and will initialize it to 11.
To get the value of your variable, you should provide and implement a method called getValue(). You use it this way:
cout sPointer.getalue(); // prints 11
You should also be able to differ the initialization of your allocated int variable to a later moment. For example the following code should be supported as well:
SmartPointer sPointer;
sPointer.setalue(133);
cout sPointer.getalue(); // prints 133
Please note that for this question, SmartPointer class will only handle allocation ofinteger type variables. Allocating arrays is not supported to keep the class simple. No reference counting is required.
The default constructor should allocate and initialize the allocated integer to zero. The destructor should take care of deleting the allocated memory.
uestion 3 (15 pts)
Exceptions are problemsencounteredduringtheexecutionoftheprogram.Forexample,if the system runs out of memory during a dynamic allocation, it will raise an exception. These exceptions should be treated whenever they are raised in order to avoid the program from yielding undefined behavior or even worst, crashing.
Add appropriate exception handling for the SmartPointer class to treat the following 2 cases:
1. When allocating a new variable, the code should throw an exception if the system runs out of memory. This exception should be treated and a message should be
3
displayed to the screen warning the userthatthevariablewasnotbeingallocated.I am aware that this cannot really be tested out of thebox,soI’mnotexpectingyour main to provide a test case for this situation. However, Iwanttoseethatyourcode “catch” theappropriateexceptionthattheoperatingsystemthrowswhenitrunsout of memory. 2. Let’s assume that we are only interested in positive numbers. The code should throw an exception whenever a user tries to assign a negative number to any variable that was being allocated through SmartPointerclass.Thisexceptionshould be treated and a message should be displayed to the screen warning the user that the class does not handle negative numbers.
uestion 4 (15 pts)
Make the classSmartPointergenericusingtemplatestobeabletoallocateanybuilt-intype through it (int, float, double). Let’s ignore char and string types for simplicity.
Use case:
SmartPointerfloat sPointer;
sPointer.setalue(13.31);
cout sPointer.getalue();
uestion 5 (2 pts)
Overload the operators +, - and*tobeabletoperformarithmeticoperationsonvariables allocated through SmartPointer class. Don’t use member methods for the overloaded functions, use friend functions instead. These overloaded functions should be generic as well (using templates).
Use case:
SmartPointerfloat sPointer1;
4
sPointer1.setalue(1.5);
SmartPointerfloat sPointer2;
sPointer2.setalue(2.5);
SmartPointerfloat sPointer3 = sPointer1 + sPointer2;
cout sPointer3.getalue() endl; // prints 4
uestion 6 (2 pts)
How can you adapt the class SmartPointer to be able to handle allocation of arrays? Please provide a full implementation. Feel free to choose any suitable signature for your class, including the way it should be used.
5