Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering the C++17 STL

You're reading from   Mastering the C++17 STL Make full use of the standard library components in C++17

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781787126824
Length 384 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Arthur O'Dwyer Arthur O'Dwyer
Author Profile Icon Arthur O'Dwyer
Arthur O'Dwyer
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Classical Polymorphism and Generic Programming 2. Iterators and Ranges FREE CHAPTER 3. The Iterator-Pair Algorithms 4. The Container Zoo 5. Vocabulary Types 6. Smart Pointers 7. Concurrency 8. Allocators 9. Iostreams 10. Regular Expressions 11. Random Numbers 12. Filesystem

Reference counting with std::shared_ptr<T>

Having completely solved the problem of memory leaks, we now tackle the problem of use-after-free bugs. The essential problem to be solved here is unclear ownership--or rather shared ownership--of a given resource or chunk of memory. This chunk of memory might have several people looking at it at different times, maybe from different data structures or from different threads, and we want to make sure that all these stakeholders are involved in the decision about when to free it. The ownership of the underlying chunk of memory should be shared.

For this, the standard has provided std::shared_ptr<T>. Its interface appears very similar to std::unique_ptr<T>; all of the differences are hidden under the hood, in the implementations of the special member functions.

std::shared_ptr<T> provides an approach to memory management...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime