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

Smart Pointers, Modern Memory Management Techniques

  • 1.
  • 2.
    • Smart pointersare 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 classtemplate 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 ifthere 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 objectshave 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 isdepreciated as should not be used.  The language has provided other smart pointers. Programming with Sikander : Smart Pointers 15
  • 16.
     Improved versionof 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 byvalue invokes copy constructor  Unique_ptr does not provide copy constructor. Programming with Sikander : Smart Pointers 19
  • 20.
     Receiving byreference will not invoke copy constructor. Programming with Sikander : Smart Pointers 20
  • 21.
     Move functiontransfers the ownership Programming with Sikander : Smart Pointers 21
  • 22.
    Programming with Sikander: Smart Pointers 22
  • 23.
    Programming with Sikander: Smart Pointers 23
  • 24.
     What isthe type of p1?  Is Memory Leaked? Programming with Sikander : Smart Pointers 24
  • 25.
    Identify the typeof p1. Verify if there is memory leak Programming with Sikander : Smart Pointers 25
  • 26.
     make_unique returnsan 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_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 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 isa 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_ptrobjects that point to a resource do not affect the resource's reference count. Programming with Sikander : Smart Pointers 31
  • 32.
     when thelast 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.