Smart Pointers, Modern Memory Management Techniques
MohammedSikander
155 views
34 slides
Jun 14, 2024
Slide 1 of 34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
About This Presentation
a
Size: 1.74 MB
Language: en
Added: Jun 14, 2024
Slides: 34 pages
Slide Content
Programming with Sikander
•
Smart pointers are a set of classes that
manage dynamically allocated objects,
providing automatic memory
management and helping to prevent
memory leaks.
Programming with Sikander : Smart Pointers 2
Programming with Sikander : Smart Pointers 3
Programming with Sikander : Smart Pointers 4
Programming with Sikander : Smart Pointers 5
Programming with Sikander : Smart Pointers 6
Programming with Sikander : Smart Pointers 7
auto_ptr
unique_ptr
shared_ptr
weak_ptr
These objects have the ability of taking ownership of
a pointer: once they take ownership they manage the
pointed object by becoming responsible for its
deletion at some point.
Programming with Sikander : Smart Pointers 8
This class template provides a garbage
collectionfacility for pointers, by allowing
pointers to have the elements they point to
automatically destroyed when the auto_ptr
object is itself destroyed.
Its defined in memoryheader file.
Programming with Sikander : Smart Pointers 9
Observe if there is any memory leakage.
Programming with Sikander : Smart Pointers 10
Programming with Sikander : Smart Pointers 11
Programming with Sikander : Smart Pointers 12
auto_ptrobjects have the peculiarity of taking
ownershipof the pointers assigned to them:
An auto_ptrobject that has ownership over one
element is in charge of destroying the element it points
to and to deallocatethe memory allocated to it when
itself is destroyed.
Therefore, no two auto_ptrobjects should ownthe
same element, since both would try to destruct them at
some point.
When an assignment operation takes place between
two auto_ptrobjects, ownershipis transferred, which
means that the object losing ownership is set to no
longer point to the element (it is set to the null pointer).
Programming with Sikander : Smart Pointers 13
Programming with Sikander : Smart Pointers 14
Auto_ptris depreciated as should not be
used.
The language has provided other smart
pointers.
Programming with Sikander : Smart Pointers 15
Improved version of auto_ptr.
unique_ptrobjects automatically delete the
object they manage as soon as they
themselves are destroyed, or as soon as their
value changes either by anassignment
operationor by an explicit call to
unique_ptr::reset.
Unique_ptrdoes not contain copy
constructor and assignment operator.
Programming with Sikander : Smart Pointers 16
Programming with Sikander : Smart Pointers 17
Programming with Sikander : Smart Pointers 18
Pass by value invokes copy constructor
Unique_ptrdoes not provide copy
constructor.
Programming with Sikander : Smart Pointers 19
Receiving by reference will not invoke
copy constructor.
Programming with Sikander : Smart Pointers 20
Move function transfers the ownership
Programming with Sikander : Smart Pointers 21
Programming with Sikander : Smart Pointers 22
Programming with Sikander : Smart Pointers 23
What is the type of p1?
Is Memory Leaked?
Programming with Sikander : Smart Pointers 24
Identify the type of p1.
Verify if there is memory leak
Programming with Sikander : Smart Pointers 25
make_uniquereturns an object of type unique_ptrand
newreturns a pointerto the created object.
unique_ptr<LongTypeName>up(new LongTypeName(args))
must mention LongTypeNametwice, while
autoup = make_unique<LongTypeName>(args)
mentions it once.
Programming with Sikander : Smart Pointers 26
Theshared_ptrtype is designed for scenarios in which
more than one owner might have to manage the
lifetime of the object in memory.
After you initialize ashared_ptryou can copy it, pass
it by value in function arguments, and assign it to
othershared_ptrinstances.
All the instances point to the same object, and share
access to one "control block" that increments and
decrements the reference count whenever a
newshared_ptris added, goes out of scope, or is
reset.
When the reference count reaches zero, the control
block deletes the memory resource and itself.
Programming with Sikander : Smart Pointers 27
Programming with Sikander : Smart Pointers 28
Programming with Sikander : Smart Pointers 29
std::weak_ptris a smart pointer that holds a
non-owning ("weak") reference to an object
that is managed by std::shared_ptr.
Programming with Sikander : Smart Pointers 30
The weak_ptrobjects that point to a resource do
not affect the resource's reference count.
Programming with Sikander : Smart Pointers 31
when the last shared_ptrobject that manages that
resource is destroyed the resource will be freed,
even if there are weak_ptrobjects pointing to that
resource.
A weak_ptrobject does not provide direct access
to the resource that it points to.
Code that needs to use the resource does so
through a shared_ptrobject that owns that
resource, created by calling the member function
lock.
Programming with Sikander : Smart Pointers 32
Programming with Sikander : Smart Pointers 33
THANK YOU
Programming with Sikander : Smart Pointers 34