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

222 views
164 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
222
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • STD :: SMARTPOINTERStalking about pointers, means dealing with memory &amp; of course memory management
  • With memory management comes issues
  • Its easy to manage small file systems, but things can get out of hand
  • When you start dealing with something like this
  • Or this
  • when you have a project of this size. It is not hard to imagine the amount of possible bugs related to shared memory,
  • Oneway of successfully handling problems of this type is by using…drumroll……
  • C++ SMART POINTERS
  • Smart pointers are Abstract Data Types that mimic regular “raw” poinrters but have an added feature called AUTOMATIC RESOURCE DEALLOCATION. Which is extremely useful in larger projects Essentially this means you will no longer have pointer associated memory issues, because the pointers will “kill” themselves.
  • So…..How do they work?
  • They work by means of operator overloadingthe behavior of traditional pointers, such as dereferencing &amp; assignment while providing extra internal memory management algorithms.They come with the 2 special TYPES that I will talk about right now… shared_ptr and unique_ptrSo to begin, lets talk about Unique_Pointer
  • Unique Pointers are as they sound “unique”, meaning that they are un-copy-able. You cant have 2 pointers pointing to the same object or place in memory.
  • Unique pointers are very easy to use and have very little overhead associated with them
  • here’s how to create a new unique pointer of type integer. &amp; here is how it looks in memory (NEXT )
  • Now in most cases you can have 2 pointers pointing to the same object. But in the case of the unique pointer the equals operator is not overloaded
  • So this case is illegal
  • However it is transferable you JUST have to use a special function called from the std called:move()As the above code does
  • Creates a unique pointer 2, points it at the pointee
  • Kills the original pointer
  • Leaving only the newly transferred pointer
  • they can also be returned
  • This function returns a local variable which will be destroyed within the function when return is read. In this case the pointer itself will call the move function just like from the previous slide, but when you return, it is done automatically. Pretty cool, returning a smart pointer in fact “moves” itself. NOW ONTO SHARED_PTR
  • Here we have the std::shared pointer. You can see right away the addition of the reference count. If you have every used languages such as objective –c there is a feature called automatic reference counting. This is the c++ version of it. Here we have created a new integer in memory and are pointing to it. And the shared pointer sets the reference count to 1.
  • It handles the reference update automatically by virtue of another pointer that points to the reference when it is created. Shared means you can actually share objects amongst pointers because they are copy constructable. Heres what happens when you actually share
  • Heres what happens when you create a second ‘shared’ pointer via the copy constructor.And each time another pointer points to the object the refrencecountt is updated. If the new pointer, shared pointer 2, should go out of scope or terminates for whatever reason.
  • The reference counter decrements automatically.
  • If the reference count gets to zero,
  • The memory gets cleared automatically
  • And the smart pointer terminates
  • Memory is completely free. Yet you have not called delete. SO, Hopefully you can see how useful smart pointers can be.
  • 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.

    ×