Smart Pointers, Modern Memory Management Techniques

MohammedSikander 155 views 34 slides Jun 14, 2024
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

a


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