際際滷

際際滷Share a Scribd company logo
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 overloading, the behavior of traditional
(raw) pointers, (e.g. dereferencing, assignment) while providing additional
memory management algorithms. ~wikipedia

std::shared_ptr
std::unique_ptr
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
pointers.
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) );
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
#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
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
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
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
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
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
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
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.
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(sharedPtr1);

*

123

sharedPtr1
2
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>
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.
Lavavej, Stephen T. "C9 Lectures:. Microsoft, n.d. Web. 06 May 2013.

C Reference. N.p., n.d. Web. 06 May 2013.

More Related Content

Brian sabbeth smart_pointers

Editor's Notes

  • #2: STD :: SMARTPOINTERStalking about pointers, means dealing with memory &amp; of course memory management
  • #3: With memory management comes issues
  • #4: Its easy to manage small file systems, but things can get out of hand
  • #5: When you start dealing with something like this
  • #6: Or this
  • #7: when you have a project of this size. It is not hard to imagine the amount of possible bugs related to shared memory,
  • #8: Oneway of successfully handling problems of this type is by usingdrumroll
  • #9: C++ SMART POINTERS
  • #10: 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.
  • #11: So..How do they work?
  • #12: 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
  • #13: 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.
  • #14: Unique pointers are very easy to use and have very little overhead associated with them
  • #15: heres how to create a new unique pointer of type integer. &amp; here is how it looks in memory (NEXT )
  • #16: 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
  • #17: So this case is illegal
  • #19: However it is transferable you JUST have to use a special function called from the std called:move()As the above code does
  • #20: Creates a unique pointer 2, points it at the pointee
  • #21: Kills the original pointer
  • #22: Leaving only the newly transferred pointer
  • #23: they can also be returned
  • #24: 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
  • #25: 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.
  • #26: 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
  • #27: 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.
  • #28: The reference counter decrements automatically.
  • #29: If the reference count gets to zero,
  • #30: The memory gets cleared automatically
  • #31: And the smart pointer terminates
  • #32: Memory is completely free. Yet you have not called delete. SO, Hopefully you can see how useful smart pointers can be.