This document discusses C++ smart pointers, which provide automatic memory management of dynamically allocated memory. It covers std::unique_ptr, which cannot be copied but can move ownership between pointers, and std::shared_ptr, which allows multiple pointers to the same memory block by using a reference count. std::unique_ptr handles returning local pointers by moving ownership, while std::shared_ptr frees memory when the reference count reaches zero. Smart pointers avoid issues with raw pointers through features like automatic deallocation.
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) );
15. 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
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.
#10: 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.
#12: 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
#13: 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.
#14: Unique pointers are very easy to use and have very little overhead associated with them
#15: heres how to create a new unique pointer of type integer. & here is how it looks in memory (NEXT )
#16: 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
#24: 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
#25: 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.
#26: 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
#27: 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.
#28: The reference counter decrements automatically.