Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Could someone please help me with this C++ lab that covers Smart Pointers? For this lab you're going to practice using smart pointers. 1. First,
Could someone please help me with this C++ lab that covers Smart Pointers?
For this lab you're going to practice using smart pointers.
1. First, create a new source file called smartpointers.cpp with main() in it. You will need to include the library to use smart pointers. Also include the library to allow us to use the STL stack. Also add the line "using namespace std;" after the includes so we don't have to use a namespace identifier. Add other libraries as needed to complete the steps below.
2. Smart pointers automatically manage memory for us. They can be used with any object we create in free store (aka the heap). First, let's create a shared pointer to an STL stack.
Recall that the STL stack is a template class, so we must specify the type the stack will store when we declare it. Let's make this stack store strings. Enter the code below:
shared_ptr> stackPtr = make_shared>();
This line creates a shared_ptr capable of pointing to a stack named stackPtr. make_shared>() creates both the manager object and the stack object on the heap. The manager object tracks how many shared pointers point to the object and automatically deletes the object when the reference count drops to zero.
3. Let's push some strings onto the stack. Push "all," "work," "no," and "play" in that order. onto the stack using push(). The syntax to call a method of an object pointed to by a smart pointer is just like a raw pointer:
stackPtr->push("all");
4. The stack method size() returns the number of items in the stack. This should be 4. Write the code to display "stackPtr's size(): " then call the stack's size() method and display the output.
5. The stack's top() function returns (but does not remove) the top item in the stack. This should be "play" because it was the last item pushed onto the stack. Write the code to display "stackPtr's top(): " then call the stack's top() method and display the output.
6. The unique() function of a shared pointer returns true if this shared pointer is the only owner of the shared object. In other words, it is true if there is only 1 shared pointer pointing to the object. That should be true at this point. Add a line to display the text "stackPtr's unique(): " then call unique() and display its output.
7. The use_count() function of a shared pointer returns the reference count for the managed object. stackPtr presently only has one reference, so use_count() should return 1. Write the code to display the text "stackPtr's use_count(): " then call use_count() and display its output.
8. Let's use auto to create another shared pointer to our stack. auto automatically detects the type that needs to be used. Create one new shared pointer called stackPtr2 like so:
auto stackPtr2 = stackPtr;
Add similar code to create another shared pointer called stackPtr3.
9. Confirm stackPtr2 and stackPtr3 are pointing to the same object as stackPtr by displaying "stackPtr2's size(): " and "stackPtr3's size(): " and calling size() on each. They should both be 4.
10. Let's add 2 weak pointers to the same object pointed to by stackPtr. Weak pointers do not effect the reference count nor can they be used to access the object. This code creates a weak pointer which doesn't yet point to anything:
weak_ptr> weakPtr;
Also add the code to create weakPtr2;
11. Now let's make both weak pointers point to the same object as stackPtr.
weakPtr = stackPtr;
Do the same with weakPtr2.
12. Now let's check stackPtr's use count again. It should now be 3 indicating 3 shared pointers. The weak pointers don't count. Display "stackPtr's use_count(): " then the output of use_count().
13. Weak pointers include the function lock(). If the object exists, lock() returns a shared pointer to the object. Otherwise it returns nullptr. Let's use auto to create one more shared pointer to our stack via lock():
auto stackPtr4 = weakPtr.lock();
14. Again display "stackPtr's use_count(): " then the output of use_count(). The count should now be 4.
15. Our stack no longer has only 1 shared pointer, so unique() should no longer be true. Confirm this by displaying the text "stackPtr's unique(): " then call unique() and display its output.
16. As we remove pointers to our stack the manager object automatically reduces the use count. Set stackPtr2, stackPtr3, and stackPtr4 to nullptr.
17. Confirm that stackPtr's use_count() is now back to 1. Display "stackPtr's use_count(): " then the output of use_count().
18. Set stackPtr to nullptr. This causes the the manager object to reducse the reference count to zero. When the reference count reaches zero the object is automatically deleted.
19. Finally, let's use weakPtr to confirm the object is really gone. weakPtr's lock() function should now return nullptr because no object is there. Write an if/else that checks to see if lock() returns nullptr and, if so, displays "lock() confirms the object no longer exists."
Confirm that your output looks like the following:
stackPtr's size(): 4
stackPtr's top(): play
stackPtr's unique(): 1
stackPtr's use_count(): 1
stackPtr2's size(): 4
stackPtr3's size(): 4
stackPtr's use_count(): 3
stackPtr's use_count(): 4
stackPtr's unique(): 0
stackPtr's use_count(): 1
lock() confirms the object no longer exists.
Place your name in a comment at the top of smartpointers.cpp and upload it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started