Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
STD::SMART POINTERS
Brian Sabbeth
May 9, 2013
MEMORY MANAGEMENT:
MEMORY MANAGEMENT:
MEMORY MANAGEMENT:
MEMORY MANAGEMENT:
MEMORY MANAGEMENT:

Amongst so many files shared objects can become a
problem for pointer related issues
MEMORY MANAGEMENT:

Amongst so many files shared objects can become a
problem for pointer related issues
SMART POINTERS
SMART POINTERS

Simulate
Pointers
AbstractData
types

Automatic
Resource
Deallocation
SMART POINTERS

Simulate
Pointers
AbstractData
types

Automatic
Resource
Deallocation
SMART POINTERS
#include <memory>

defines general utilities to manage
dynamic memory:

“They work by means of operator ove...
STD::UNIQUE_PTR
#include <memory>
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
New objects can be moved or transferred between smart...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
New objects can be moved or transferred between smart...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
#include <memory>
Unique – Not allowed to be copied!
But can be moved.
EASY TO IMPLEMENT:
std::unique_ptr<...
STD::UNIQUE_PTR
Unique Pointers are returnable
EASILY DONE!
std::unique_ptr<int>returnUnique(intx);
{
unique_ptr<int>my_un...
STD::SHARED_PTR
#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

*

123

sharedPtr1
1
Ref Count
STD::SHARED_PTR
#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

*

123

sharedPtr1
1
Ref Count
STD::SHARED_PTR
#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );
std::shared_ptr<int> sharedPtr2(sharedP...
STD::SHARED_PTR
#include <memory>

std::shared_ptr<int> sharedPtr1(new int(123) );

*

123

sharedPtr1
1
Ref Count
STD::SHARED_PTR
#include <memory>
Shared!

123
0
Ref Count
STD::SHARED_PTR
#include <memory>
Shared!

0
Ref Count
STD::SHARED_PTR
#include <memory>
Memory is free
STD::SHARED_PTR
STD::SMART_POINTERS
Bibliography:
"Smart Pointer." Wikipedia. Wikimedia Foundation, 04 June 2013. Web.
06 May 2013.
Lavave...
Upcoming SlideShare
Loading in …5
×

Brian sabbeth smart_pointers

339 views

Published on

  • Be the first to comment

  • Be the first to like this

Brian sabbeth smart_pointers

  1. 1. STD::SMART POINTERS Brian Sabbeth May 9, 2013
  2. 2. MEMORY MANAGEMENT:
  3. 3. MEMORY MANAGEMENT:
  4. 4. MEMORY MANAGEMENT:
  5. 5. MEMORY MANAGEMENT:
  6. 6. MEMORY MANAGEMENT: Amongst so many files shared objects can become a problem for pointer related issues
  7. 7. MEMORY MANAGEMENT: Amongst so many files shared objects can become a problem for pointer related issues
  8. 8. SMART POINTERS
  9. 9. SMART POINTERS Simulate Pointers AbstractData types Automatic Resource Deallocation
  10. 10. SMART POINTERS Simulate Pointers AbstractData types Automatic Resource Deallocation
  11. 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
  12. 12. STD::UNIQUE_PTR #include <memory>
  13. 13. STD::UNIQUE_PTR #include <memory> Unique – Not allowed to be copied! New objects can be moved or transferred between smart pointers.
  14. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.”
  24. 24. STD::SHARED_PTR #include <memory> std::shared_ptr<int> sharedPtr1(new int(123) ); * 123 sharedPtr1 1 Ref Count
  25. 25. STD::SHARED_PTR #include <memory> std::shared_ptr<int> sharedPtr1(new int(123) ); * 123 sharedPtr1 1 Ref Count
  26. 26. STD::SHARED_PTR #include <memory> std::shared_ptr<int> sharedPtr1(new int(123) ); std::shared_ptr<int> sharedPtr2(sharedPtr1); * 123 sharedPtr1 2 Ref Count
  27. 27. STD::SHARED_PTR #include <memory> std::shared_ptr<int> sharedPtr1(new int(123) ); * 123 sharedPtr1 1 Ref Count
  28. 28. STD::SHARED_PTR #include <memory> Shared! 123 0 Ref Count
  29. 29. STD::SHARED_PTR #include <memory> Shared! 0 Ref Count
  30. 30. STD::SHARED_PTR #include <memory> Memory is free
  31. 31. STD::SHARED_PTR
  32. 32. STD::SMART_POINTERS Bibliography: "Smart Pointer." Wikipedia. Wikimedia Foundation, 04 June 2013. Web. 06 May 2013. Lavavej, Stephen T. "C9 Lectures:.” Microsoft, n.d. Web. 06 May 2013. C Reference. N.p., n.d. Web. 06 May 2013.

×