brian sabbeth smart_pointers
Post on 18-Jul-2015
134 Views
Preview:
TRANSCRIPT
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
AbstractDatatypes
Simulate Pointers
Automatic Resource Deallocation
SMART POINTERS
AbstractDatatypes
Simulate Pointers
Automatic Resource Deallocation
SMART POINTERS
#include <memory> defines general utilities to manage
dynamic memory:
std::shared_ptr
std::unique_ptr
“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::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!
std::unique_ptr<int>uniqPtr 1(new int(123) );
New objects can be moved or transferred between smart
pointers.
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) );
123
*
uniqPtr1
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) );
123
*
uniqPtr1
*
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) );
123
*
uniqPtr1
*
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) );
123
*
uniqPtr1
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
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) );
123
*
uniqPtr1
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
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) );
123
*
uniqPtr1
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
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) );
123
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
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) );
123
std::unique_ptr<int> uniqPtr2 = std::move(uniqPtr1);
uniqPtr2
*
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) );
123sharedPtr1
1
Ref Count
*
STD::SHARED_PTR
#include <memory>
std::shared_ptr<int> sharedPtr1(new int(123) );
123sharedPtr1
1
Ref Count
*
STD::SHARED_PTR
#include <memory>
std::shared_ptr<int> sharedPtr1(new int(123) );
123sharedPtr1
2
Ref Count
std::shared_ptr<int> sharedPtr2(sharedPtr1);
*
STD::SHARED_PTR
#include <memory>
std::shared_ptr<int> sharedPtr1(new int(123) );
123sharedPtr1
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:
Lavavej, Stephen T. "C9 Lectures:.” Microsoft, n.d. Web. 06 May 2013.
"Smart Pointer." Wikipedia. Wikimedia Foundation, 04 June 2013. Web.
06 May 2013.
C Reference. N.p., n.d. Web. 06 May 2013.
top related