11. SMART POINTERS
#include <memory>
defines general utilities to manage
dynamic memory:
“They work by means of operator overloading, the behavior of traditional
(raw) pointers, (e.g. dereferencing, assignment) while providing additional
memory management algorithms.” ~wikipedia
std::shared_ptr
std::unique_ptr
14. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
New objects can be moved or transferred between smart
pointers.
std::unique_ptr<int>uniqPtr 1(new int(123) );
16. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
*
uniqPtr1
123
*
std::unique_ptr<int> uniqPtr2 = uniqPtr1;
uniqPtr2
17. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
*
uniqPtr1
123
*
std::unique_ptr<int> uniqPtr2 = uniqPtr1;
uniqPtr2
18. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
*
uniqPtr1
123
19. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
*
uniqPtr1
123
20. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
*
uniqPtr1
123
21. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
123
22. STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<int> uniqPtr1(new int(123) );
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
*
uniqPtr2
123
23. STD::UNIQUE_PTR
Unique Pointers are returnable
EASILY DONE!
std::unique_ptr<int>returnUnique(intx);
{
unique_ptr<int>my_unique_ptr(newint(x));
return my_unique_ptr;
}
This returns a local variable which will be
destroyed within the function when return is
read
The pointer itself handles the return as if it is
a “move.”
STD :: SMARTPOINTERStalking about pointers, means dealing with memory & of course memory management
With memory management comes issues
Its easy to manage small file systems, but things can get out of hand
When you start dealing with something like this
Or this
when you have a project of this size. It is not hard to imagine the amount of possible bugs related to shared memory,
Oneway of successfully handling problems of this type is by using…drumroll……
C++ SMART POINTERS
Smart pointers are Abstract Data Types that mimic regular “raw” poinrters but have an added feature called AUTOMATIC RESOURCE DEALLOCATION. Which is extremely useful in larger projects Essentially this means you will no longer have pointer associated memory issues, because the pointers will “kill” themselves.
So…..How do they work?
They work by means of operator overloadingthe behavior of traditional pointers, such as dereferencing & assignment while providing extra internal memory management algorithms.They come with the 2 special TYPES that I will talk about right now… shared_ptr and unique_ptrSo to begin, lets talk about Unique_Pointer
Unique Pointers are as they sound “unique”, meaning that they are un-copy-able. You cant have 2 pointers pointing to the same object or place in memory.
Unique pointers are very easy to use and have very little overhead associated with them
here’s how to create a new unique pointer of type integer. & here is how it looks in memory (NEXT )
Now in most cases you can have 2 pointers pointing to the same object. But in the case of the unique pointer the equals operator is not overloaded
So this case is illegal
However it is transferable you JUST have to use a special function called from the std called:move()As the above code does
Creates a unique pointer 2, points it at the pointee
Kills the original pointer
Leaving only the newly transferred pointer
they can also be returned
This function returns a local variable which will be destroyed within the function when return is read. In this case the pointer itself will call the move function just like from the previous slide, but when you return, it is done automatically. Pretty cool, returning a smart pointer in fact “moves” itself. NOW ONTO SHARED_PTR
Here we have the std::shared pointer. You can see right away the addition of the reference count. If you have every used languages such as objective –c there is a feature called automatic reference counting. This is the c++ version of it. Here we have created a new integer in memory and are pointing to it. And the shared pointer sets the reference count to 1.
It handles the reference update automatically by virtue of another pointer that points to the reference when it is created. Shared means you can actually share objects amongst pointers because they are copy constructable. Heres what happens when you actually share
Heres what happens when you create a second ‘shared’ pointer via the copy constructor.And each time another pointer points to the object the refrencecountt is updated. If the new pointer, shared pointer 2, should go out of scope or terminates for whatever reason.
The reference counter decrements automatically.
If the reference count gets to zero,
The memory gets cleared automatically
And the smart pointer terminates
Memory is completely free. Yet you have not called delete. SO, Hopefully you can see how useful smart pointers can be.