In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use C++Smart Pointer smart pointer". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use C++Smart Pointer smart pointer.
First, why use smart pointers? smart pointers in the standard library: std::auto_ptr-- single ownership (appears in Category 98, has many defects, is abandoned) std::unique_ptr-- single ownership (Category 11 replaces std::auto_ptr, for single threading) std::shared_ptr-shared ownership (Clipper 11, for multithreading) std::weak_ptr-temp/no ownership (Clipper 11) Introduced in C++ 11Defined in header.
First of all, look at a chestnut below. On the left, there is no use of smart pointers. When you execute the foo () function, the e pointer will be passed into the bar function when bar (e), but when there is no delete e after the end of the bar function, it will cause a memory leak; but in the chestnut on the right, using the unique_ptr smart pointer (single ownership) can prevent memory leaks.
Smart pointers are mainly used to manage memory allocated on the heap, encapsulating ordinary pointers as a stack object. When the stack object's lifetime ends, the requested memory is freed in the destructor to prevent memory leaks.
Auto_ptr smart pointer: (this is the only smart pointer before Clipper 11 comes out.) when the object is copied or assigned, the previous object is suspended.
Unique_ptr smart pointers: prevent smart pointers from copying and copying.
Shared_ptr smart pointer: share resources among multiple shared_ptr objects by reference counting.
Weak_ptr smart pointer: can be constructed from one shared_ptr or another weak_ptr object, and its construction and destructions do not cause an increase or decrease in reference counting.
Note: each smart pointer can increase the reference count of memory.
Smart pointers fall into two categories:
One is that you can use multiple smart pointers to manage the same memory area. Each additional smart pointer increases the reference count.
The other is that you cannot use multiple smart pointers to manage the same area of memory. Generally speaking, when smart pointer 2 manages this piece of memory, the original smart pointer 1 that manages this piece of memory can only release ownership of the pointer (ownership).
According to this classification standard, auto_ptr unique_ptr weak_ptr belongs to the latter and shared_ptr belongs to the former.
When initializing shared_ptr, you cannot assign a normal pointer directly to a smart pointer, because one is a pointer and the other is a class. You can pass in a normal pointer through the make_shared function or through the constructor. And you can get the normal pointer through the get function.
# include # include using namespace std;class report {private: string str;public: report (const string s): str (s) / / Construction method {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.