SlideShare une entreprise Scribd logo
1  sur  32
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.

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Brian sabbeth smart_pointers

Notes de l'éditeur

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