SlideShare a Scribd company logo
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
collection facility 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 memory header 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_ptr objects have the peculiarity of taking
ownership of the pointers assigned to them:
 An auto_ptr object that has ownership over one
element is in charge of destroying the element it points
to and to deallocate the memory allocated to it when
itself is destroyed.
 Therefore, no two auto_ptr objects should own the
same element, since both would try to destruct them at
some point.
 When an assignment operation takes place between
two auto_ptr objects, ownership is 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_ptr is 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_ptr objects automatically delete the
object they manage as soon as they
themselves are destroyed, or as soon as their
value changes either by an assignment
operation or by an explicit call to
unique_ptr::reset.
 Unique_ptr does 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_ptr does 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_unique returns an object of type unique_ptr and
new returns a pointer to the created object.
 unique_ptr<LongTypeName> up(new LongTypeName(args))
must mention LongTypeName twice, while
auto up = make_unique<LongTypeName>(args)
mentions it once.
Programming with Sikander : Smart Pointers 26
 The shared_ptr type is designed for scenarios in which
more than one owner might have to manage the
lifetime of the object in memory.
 After you initialize a shared_ptr you can copy it, pass
it by value in function arguments, and assign it to
other shared_ptr instances.
 All the instances point to the same object, and share
access to one "control block" that increments and
decrements the reference count whenever a
new shared_ptr is 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_ptr is 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_ptr objects that point to a resource do
not affect the resource's reference count.
Programming with Sikander : Smart Pointers 31
 when the last shared_ptr object that manages that
resource is destroyed the resource will be freed,
even if there are weak_ptr objects pointing to that
resource.
 A weak_ptr object does not provide direct access
to the resource that it points to.
 Code that needs to use the resource does so
through a shared_ptr object that owns that
resource, created by calling the member function
lock.
Programming with Sikander : Smart Pointers 32
Programming with Sikander : Smart Pointers 33
THANKYOU
Programming with Sikander : Smart Pointers 34

More Related Content

Similar to Smart Pointers, Modern Memory Management Techniques (20)

PPTX
Forging Trusts for Deception in Active Directory
Nikhil Mittal
 
PDF
cf.Objective() 2017 - Design patterns - Brad Wood
Ortus Solutions, Corp
 
PDF
Smart Pointers in C++
Francesco Casalegno
 
PDF
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
noorcon
 
PPTX
[StockDice] Application for Stock Exchange Monitoring And Business Intelligen...
Kiran Munir
 
PPTX
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
Simplilearn
 
PPTX
Internship (7)gfytfyugiujhoiipobjhvyuhjkb jh
sidd233245456df
 
PPTX
Internship (7)szgsdgszdssagsagzsvszszvsvszfvsz
sidd233245456df
 
PPTX
Effective Java
Brice Argenson
 
PDF
The Evolution of Distributed Systems on Kubernetes
Bilgin Ibryam
 
PDF
Design patterns through java
Aditya Bhuyan
 
PDF
Assignment 4- Implementing a Banking Application Due See course shel.pdf
amarhandi
 
PPT
8. design patterns
APU
 
PDF
Understand, verify, and act on the security of your Kubernetes clusters - Sca...
Scaleway
 
PDF
Advanced View Arduino Projects List - Use Arduino for Projects-2.pdf
WiseNaeem
 
PDF
Transforming Feature Ideas into Machine Learning Inputs
FeatureByte
 
PDF
Acd19 kubertes cluster at scale on aws at intuit
John Varghese
 
PDF
Supporting Highly Multitenant Spark Notebook Workloads with Craig Ingram and ...
Spark Summit
 
PPTX
smart.pptxsmart.pptxsmart.pptxsmart.pptxsmart.pptx
atharvahambarde1
 
PPTX
smart.pptxsmart.pptxsmart.pptxsmart.pptxsmart.pptx
atharvahambarde1
 
Forging Trusts for Deception in Active Directory
Nikhil Mittal
 
cf.Objective() 2017 - Design patterns - Brad Wood
Ortus Solutions, Corp
 
Smart Pointers in C++
Francesco Casalegno
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
noorcon
 
[StockDice] Application for Stock Exchange Monitoring And Business Intelligen...
Kiran Munir
 
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
Simplilearn
 
Internship (7)gfytfyugiujhoiipobjhvyuhjkb jh
sidd233245456df
 
Internship (7)szgsdgszdssagsagzsvszszvsvszfvsz
sidd233245456df
 
Effective Java
Brice Argenson
 
The Evolution of Distributed Systems on Kubernetes
Bilgin Ibryam
 
Design patterns through java
Aditya Bhuyan
 
Assignment 4- Implementing a Banking Application Due See course shel.pdf
amarhandi
 
8. design patterns
APU
 
Understand, verify, and act on the security of your Kubernetes clusters - Sca...
Scaleway
 
Advanced View Arduino Projects List - Use Arduino for Projects-2.pdf
WiseNaeem
 
Transforming Feature Ideas into Machine Learning Inputs
FeatureByte
 
Acd19 kubertes cluster at scale on aws at intuit
John Varghese
 
Supporting Highly Multitenant Spark Notebook Workloads with Craig Ingram and ...
Spark Summit
 
smart.pptxsmart.pptxsmart.pptxsmart.pptxsmart.pptx
atharvahambarde1
 
smart.pptxsmart.pptxsmart.pptxsmart.pptxsmart.pptx
atharvahambarde1
 

More from Mohammed Sikander (20)

PPTX
Strings in C - covers string functions
Mohammed Sikander
 
PDF
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
PDF
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
PDF
Operator Overloading in C++
Mohammed Sikander
 
PDF
Python_Regular Expression
Mohammed Sikander
 
PPTX
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
PDF
Modern_cpp_auto.pdf
Mohammed Sikander
 
PPTX
Python Functions
Mohammed Sikander
 
PPTX
Python dictionary
Mohammed Sikander
 
PDF
Python exception handling
Mohammed Sikander
 
PDF
Python tuple
Mohammed Sikander
 
PDF
Python strings
Mohammed Sikander
 
PDF
Python set
Mohammed Sikander
 
PDF
Python list
Mohammed Sikander
 
PDF
Python Flow Control
Mohammed Sikander
 
PDF
Introduction to Python
Mohammed Sikander
 
PPTX
Pointer basics
Mohammed Sikander
 
PPTX
Signal
Mohammed Sikander
 
PPTX
File management
Mohammed Sikander
 
Strings in C - covers string functions
Mohammed Sikander
 
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Operator Overloading in C++
Mohammed Sikander
 
Python_Regular Expression
Mohammed Sikander
 
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Mohammed Sikander
 
Python dictionary
Mohammed Sikander
 
Python exception handling
Mohammed Sikander
 
Python tuple
Mohammed Sikander
 
Python strings
Mohammed Sikander
 
Python set
Mohammed Sikander
 
Python list
Mohammed Sikander
 
Python Flow Control
Mohammed Sikander
 
Introduction to Python
Mohammed Sikander
 
Pointer basics
Mohammed Sikander
 
File management
Mohammed Sikander
 
Ad

Recently uploaded (20)

PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Ad

Smart Pointers, Modern Memory Management Techniques

  • 2. • 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
  • 3. Programming with Sikander : Smart Pointers 3
  • 4. Programming with Sikander : Smart Pointers 4
  • 5. Programming with Sikander : Smart Pointers 5
  • 6. Programming with Sikander : Smart Pointers 6
  • 7. Programming with Sikander : Smart Pointers 7
  • 8.  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
  • 9.  This class template provides a garbage collection facility 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 memory header file. Programming with Sikander : Smart Pointers 9
  • 10.  Observe if there is any memory leakage. Programming with Sikander : Smart Pointers 10
  • 11. Programming with Sikander : Smart Pointers 11
  • 12. Programming with Sikander : Smart Pointers 12
  • 13.  auto_ptr objects have the peculiarity of taking ownership of the pointers assigned to them:  An auto_ptr object that has ownership over one element is in charge of destroying the element it points to and to deallocate the memory allocated to it when itself is destroyed.  Therefore, no two auto_ptr objects should own the same element, since both would try to destruct them at some point.  When an assignment operation takes place between two auto_ptr objects, ownership is 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
  • 14. Programming with Sikander : Smart Pointers 14
  • 15.  Auto_ptr is depreciated as should not be used.  The language has provided other smart pointers. Programming with Sikander : Smart Pointers 15
  • 16.  Improved version of auto_ptr.  unique_ptr objects automatically delete the object they manage as soon as they themselves are destroyed, or as soon as their value changes either by an assignment operation or by an explicit call to unique_ptr::reset.  Unique_ptr does not contain copy constructor and assignment operator. Programming with Sikander : Smart Pointers 16
  • 17. Programming with Sikander : Smart Pointers 17
  • 18. Programming with Sikander : Smart Pointers 18
  • 19.  Pass by value invokes copy constructor  Unique_ptr does not provide copy constructor. Programming with Sikander : Smart Pointers 19
  • 20.  Receiving by reference will not invoke copy constructor. Programming with Sikander : Smart Pointers 20
  • 21.  Move function transfers the ownership Programming with Sikander : Smart Pointers 21
  • 22. Programming with Sikander : Smart Pointers 22
  • 23. Programming with Sikander : Smart Pointers 23
  • 24.  What is the type of p1?  Is Memory Leaked? Programming with Sikander : Smart Pointers 24
  • 25. Identify the type of p1. Verify if there is memory leak Programming with Sikander : Smart Pointers 25
  • 26.  make_unique returns an object of type unique_ptr and new returns a pointer to the created object.  unique_ptr<LongTypeName> up(new LongTypeName(args)) must mention LongTypeName twice, while auto up = make_unique<LongTypeName>(args) mentions it once. Programming with Sikander : Smart Pointers 26
  • 27.  The shared_ptr type is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory.  After you initialize a shared_ptr you can copy it, pass it by value in function arguments, and assign it to other shared_ptr instances.  All the instances point to the same object, and share access to one "control block" that increments and decrements the reference count whenever a new shared_ptr is 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
  • 28. Programming with Sikander : Smart Pointers 28
  • 29. Programming with Sikander : Smart Pointers 29
  • 30.  std::weak_ptr is 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
  • 31.  The weak_ptr objects that point to a resource do not affect the resource's reference count. Programming with Sikander : Smart Pointers 31
  • 32.  when the last shared_ptr object that manages that resource is destroyed the resource will be freed, even if there are weak_ptr objects pointing to that resource.  A weak_ptr object does not provide direct access to the resource that it points to.  Code that needs to use the resource does so through a shared_ptr object that owns that resource, created by calling the member function lock. Programming with Sikander : Smart Pointers 32
  • 33. Programming with Sikander : Smart Pointers 33
  • 34. THANKYOU Programming with Sikander : Smart Pointers 34