In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you about the shared_ptr of C++ and how to quote the right value. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Introduction
There is no garbage collection mechanism in C++, so you must release the allocated memory by yourself, otherwise it will cause memory leak. The most effective way to solve this problem is to use smart pointers (smart pointer). Smart pointers are classes that store pointers to dynamically allocated (heap) objects and are used for lifetime control to ensure that dynamically allocated objects are automatically destroyed when leaving the scope of the pointer to prevent memory leaks. The core implementation technology of smart pointer is reference counting, each time it is used, the internal reference count is increased by 1, and when the internal reference count is reduced to 0, the pointed heap memory is deleted.
There are three kinds of smart pointers available in Clippers 11, and header files need to be referenced when using them:
Std::shared_ptr: shared smart pointer
Std::unique_ptr: exclusive smart pointer
Std::weak_ptr: a weakly referenced smart pointer that does not share pointers, cannot manipulate resources, and is used to monitor shared_ptr.
Shared smart pointer (shared_ptr) means that multiple smart pointers can manage the same valid memory at the same time. Shared smart pointer shared_ptr is a template class. If you want to initialize it, there are three ways: through constructor, std::make_shared helper function and reset method. After the shared smart pointer object is initialized, it points to the heap memory to be managed. If you want to see how many smart pointers are managing this memory at the same time, you can use a member function use_count provided by the shared smart pointer.
two。 Initialization method 2.1 initialization by constructor
Example
/ / use smart pointers to manage a piece of int heap memory shared_ptr ptr1 (new int); 2.2 initialize by copying and moving constructors
Call the copy constructor
Shared_ptr ptr2 (ptr1)
Call the mobile constructor
Std::shared_ptr ptr5 = std::move (ptr2)
If you initialize a shared smart pointer object using a copy, the two objects will manage the same heap memory at the same time, and the reference count corresponding to the heap memory will be increased.
If the initial smart pointer object is moved, only the ownership of the memory is transferred, the number of objects managing the memory will not increase, so the reference count of the memory will not change.
2.2.1 Mobile Construction
With regard to mobile construction, some readers may not understand
Mobile construction is a new construction method provided in the standard Category 11.
There are many examples of this in reality, where we transfer money from one account to another, transfer mobile phone SIM cards to another phone, and cut files from one location to another. Moving constructs can reduce unnecessary replication and lead to performance improvements.
Let's first take a look at the move function.
First of all, look at a piece of code like this.
# include # include using namespace std;int main () {string st = "I love sir"; vector vc; vc.push_back (move (st)); 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.