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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to solve the garbage collection problem on C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
C++ citation counting design and analysis (solving garbage collection problem) 1. Introduction
We learned that when we make a shallow copy, there will be a memory leak when copying objects with pointers. Can C++ be introduced like python,JAVA?
Garbage collection mechanism to manage memory flexibly.
Unfortunately, C++ does not have a garbage collection mechanism (Gabage Collector) like python, java and other programming languages, which leads to dynamic changes in C++.
Storage management is called a programmer's nightmare, resulting in memory loss (memory leak), dangling pointers, illegal pointer access and other problems.
Bjarne himself believes that:
"I intend to design Cgarbage collection so that it doesn't rely on automatic garbage collection (usually just garbage collection). This is based on my own experience with garbage collection systems.
I am afraid of the serious space and time overhead, as well as the complexity of implementing and porting garbage collection systems. Also, garbage collection will make C +
-it is not suitable to do a lot of low-level work, which is one of its design goals. But I like the idea of garbage collection, which is a mechanism that simplifies design,
Rule out many sources of errors.
The constructors and destructors provided in C++ are designed to address the need for automatic release of resources. Bjarne has a famous saying, "Resource requirements are Resource Inquirment Is Initialization."
Therefore, we can apply for completion of the resources that need to be allocated in the constructor and release the allocated resources in the destructor.
In C++, which allows you to control the creation, clarity, and replication of objects, we can achieve this control by developing a garbage collection mechanism called reference counting
two。 Design thought
First of all, we make it clear that when constructing an object with a pointer, we need to delete the pointer (release) when destructing the object, but at this point if we make a shallow copy of the object, there is no new pointer new.
A memory leak occurs when an object is destructed (the memory referred to by a pointer is freed twice). We use reference counting to solve this problem:
Each time an object is constructed, a new counter is created and + 1. Each copy is constructed once on the counter where the object is copied.
Destruct in the order in which the constructor is destructed (after building and releasing first, similar to stack), and the last construction or copy is released first.
Each time you release the counter-1 and determine whether the counter is 0 (whether there is a shallow copy of the object), and when it is greater than 0, continue to destruct the next object according to the destructing order.
When the counter is 0, release the pointer.
3. Give an example
We construct three objects in order, and the counter is marked with 1pm and 2pm. We make shallow copies of the first and third objects twice.
After copying the object, the value of counter 1, 2, 2, 3 is 21. 2.
First release counter 3 counter-1 and then equal to 1, destructing an object. The counter is 2 1 1
Then release counter 1, counter-1 equals 1, and destructs an object. The counter is 1 1 1
Release counter 3, counter-1, which equals 0, destructs an object, and releases the pointer. Counter is 1 1 empty
Release counter 2, counter-1, which equals 0, destructs an object, and releases the pointer. Counter is 1 empty
Then release counter 1 counter-1 equals 0, destruct an object, and release the pointer. Counter is empty
Finally, all the objects are destructed and the pointers are released
4. Code / / reference count class class CRefCount {public: CRefCount (); / / construct counter object CRefCount (const CRefCount& obj); / / copy construction counter void* Alloc (int size); / / apply for space int AddRef () when constructing object; / / count increase int ReleaseRef (); / / count decrease ~ CRefCount () Private: void* mroompBuf; / / pointer buffer int* mroompRefCount; / / count}; CRefCount::CRefCount () {m_pBuf = nullptr; m_pRefCount = nullptr;} CRefCount::CRefCount (const CRefCount& obj) {m_pBuf = obj.m_pBuf; m_pRefCount = obj.m_pRefCount; AddRef () } void* CRefCount::Alloc (int size) {m_pBuf = new char [size + 1]; / / apply for buffer m_pRefCount = new int (0); AddRef (); / / per construction object count + 1 return mSecretpBuf;} int CRefCount::AddRef () {if (m_pRefCount = = nullptr) return 0; return + (* m_pRefCount) } int CRefCount::ReleaseRef () {if (m_pRefCount = = nullptr) return 0; return-- (* m_pRefCount);} CRefCount::~CRefCount () {if (ReleaseRef () = = 0) {if (m_pBuf! = nullptr) {delete [] m_pBuf M_pBuf = nullptr; delete masked pRefCount; m_pRefCount = nullptr;}} 5. Test / / student test case # include "CRefCount.h" # include#pragma warning (disable:4996) using namespace std;class CStudent {private: char* mSecretpName; CRefCount masked RefCount; const char* GetName () const;public: CStudent (const char* pName);}; const char* CStudent::GetName () const {return m_pName } CStudent::CStudent (const char* pName) {m_pName = (char*) m_RefCount.Alloc (strlen (pName) + 1); / / apply for a space strcpy (m_pName, pName) for storing names;} int main () {CStudent S1 ("shadow"); CStudent S2 ("iceice"); CStudent S3 ("maybe"); CStudent S4 = S1; CStudent S5 = S3 Return 0;}
After debugging this program, we look at the memory after completing the construction and copying, and we can see that the corresponding values of counter 1meme 2p3 at this time are 2meme 1p2 respectively.
If you follow in a single step, you can see that the object constructed by the first copy is destructed, and the counter value is-1. At this time, the three counter values are 2, 1, and 1, respectively.
Then go back and find that the memory referred to by the deconstructing pointer of the second copy object has not been freed. Counter 1-1, at this time, the counter value is 1, 1, 1, and 1.
Execute backwards, when the third constructed object starts to be destructed and the counter is reduced to 0, and the pointer to object 3 is released.
Add the auxiliary debugging code, and finally you can see the execution result, construct 3 times, copy 2 times, release 3 times, and complete the reference counting function.
This is the end of the introduction of "C++ how to solve the garbage collection problem". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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
I used to use GridView and Acti to do paging.
© 2024 shulou.com SLNews company. All rights reserved.