First of all, why use smart pointer . Smart pointer is to solve the problem of memory leakage .C/C++ Programmers can build their own stacks (heap) Memory space on , At the same time, you need to free the memory space on the heap by yourself . Once you forget to free up memory , This can cause memory leaks .

    I have to mention it RAII mechanism ( Resource acquisition is initialization ,Resource Acquisition Is
Initialization). Apply for resources in the class's constructor , Then use , Finally, release resources in the destructor . So that's why destructors need to use virtual To prevent memory leaks .

     There are two kinds of computers: stack and heap .

     Here's a point .C++ Objects in (class) All built-in objects (int,float,double Equal and external ( User defined objects ) object . Everything is the object .

If the corresponding class, that OS The corresponding memory space will be automatically released , therefore RAII The mechanism works normally , When leaving the corresponding scope ,class Will automatically call its own destructor to release resources . But if new Method to create on the heap class, that class Does not call its own destructor . Programmers must use delete To destroy it . Otherwise, it will cause memory leak .

     Here are two more tips , What I was asked about when I interviewed Tencent outsourcing .

*
new,delete Mechanism of .

*
new Throw exceptions and how to new Do not throw exceptions .

    1.new There are two steps :(1) call operator new() Function opens up memory space .(2) call class Constructor for .

delete There are also two steps :(1) call class Destructor of .(2) call operator delete() Free memory .

    2.new Exception thrown . To prevent throwing exceptions . You should use std::nothrow. example :

 char *ptr = new(std::nothrow) char [20];

 

     Introduction to smart pointer , They are all included in the header file #include <memory> in . It mainly solves the problem of automatic release of some acquired resources .

*
unique_ptr 

     Exclusive smart pointer , Copy replication not allowed , Copy construction not allowed . It's not just a proxy new Individual objects created , Can also act as an agent new[] Array of objects created .

     The code is as follows :
/// /// @file unique.cpp /// @author kogan(709954896@qq.com) /// @date
2020-06-21 09:28:02 /// #include <iostream> #include <memory> #include
<utility> using std::cout; using std::endl; int main() {
//make_unique stay C++11 Missing in , stay C++14 Add to . So use -std=c++14 // establish int Type pointer . // Create using factory method
std::unique_ptr<int> ptr = std::make_unique<int>(5); // It can also be created directly during initialization
std::unique_ptr<int> ptr2(new int(6)); //std::unique_ptr<int> ptr3(ptr);
// Copy construction not allowed //std::unique_ptr<int> ptr3 = ptr;// Assignment not allowed // establish int Type dynamic array
std::unique_ptr<int[]> ptrArray = std::make_unique<int[]>(20); cout << "*ptr: "
<< *ptr << endl; // Created unique Dynamic array , You can use the following table to access . ptrArray[2] = 3; cout <<
ptrArray[2] << endl; // Wrong usage , Can compile successfully , But there will be problems . //std::unique_ptr<int> ptr5(new
int[20]); return 0; }
*
shared_ptr

     Reference counting smart pointer . Is the most valuable , above all , Most useful components . Free to copy and assign . When this pointer points to a certain pointer , Add one to the reference count . If it's from another smart_ptr Get the management right of a pointer there , Two smart_ptr Pointer will reference count plus one . When the reference count is 0 Time ,smart_ptr The memory space pointed to by the pointer will be deleted .

     The code is as follows :
/// /// @file shared.cpp /// @author kogan(709954896@qq.com) /// @date
2020-06-21 11:51:39 /// #include <iostream> #include <memory> using std::cout;
using std::endl; int main() { // Initialization mode std::shared_ptr<int> ptr(new int(3));
std::shared_ptr<int> ptr2 = std::make_shared<int>(5); // take ptr2 Managed pointer assigned to ptr3
std::shared_ptr<int> ptr3(ptr2); cout << ptr3.use_count() << endl; return 0; }
Interview focus : Circular reference count .

Circular reference count : Two smart_ptr The hands point at each other , Cause memory leak . You need to use weak_ptr.

     The code is as follows :

*
weak_ptr

Weak reference pointer . For observation shared_ptr or weak_ptr. Used to resolve circular reference count . because weak_ptr No shared resources , Its constructor does not cause the pointer reference count to change .

 
/// /// @file circle_reference.cpp /// @author kogan(709954896@qq.com) ///
@date 2020-06-21 12:04:48 /// #include <iostream> #include <memory> using
std::cout; using std::endl; using std::shared_ptr; class Child;// forward declaration class
Parent { public: Parent(){ cout << "Parent()" << endl; } ~Parent() { cout <<
"~Parent()" << endl; } shared_ptr<Child> m_Child; }; class Child { public:
Child(){ cout << "Child()" << endl; } ~Child(){ cout << "~Child()" << endl; }
shared_ptr<Parent> m_Parent; }; int main(void) { //shared_ptr Circular references to cause memory leaks
shared_ptr<Parent> parent(new Parent); shared_ptr<Child> child(new Child); cout
<< "sizeof(shared_ptr) = " << sizeof(parent) << endl; cout << "parent 's
use_count() = " << parent.use_count() << endl; cout << "child's use_count() = "
<< child.use_count() << endl; parent->m_Child = child;// assignment child->m_Parent =
parent; cout << "parent 's use_count() = " << parent.use_count() << endl; cout
<< "child's use_count() = " << child.use_count() << endl; return 0; }
use weak_ptr No memory leak after .

 
/// /// @file circle_reference2.cpp /// @author kogan(709954896@qq.com) ///
@date 2020-06-21 12:09:37 /// #include <iostream> #include <memory> using
std::cout; using std::endl; using std::shared_ptr; using std::weak_ptr; class
Child;// forward declaration class Parent { public: Parent(){ cout << "Parent()" << endl; }
~Parent() { cout << "~Parent()" << endl; } shared_ptr<Child> m_Child; }; class
Child { public: Child(){ cout << "Child()" << endl; } ~Child(){ cout <<
"~Child()" << endl; } //shared_ptr<Parent> m_Parent; weak_ptr<Parent> m_Parent;
}; int main(void) { //shared_ptr Circular references to cause memory leaks // // The solution is to set one of the pointers to weak_ptr
//weak_ptr When doing assignment , Does not change the value of the reference count shared_ptr<Parent> parent(new Parent);
shared_ptr<Child> child(new Child); cout << "sizeof(shared_ptr) = " <<
sizeof(parent) << endl; cout << "parent 's use_count() = " <<
parent.use_count() << endl; cout << "child's use_count() = " <<
child.use_count() << endl; parent->m_Child = child;// assignment child->m_Parent =
parent;// weak_ptr = shared_ptr; cout << endl << " After the assignment :" << endl; cout <<
"parent 's use_count() = " << parent.use_count() << endl; cout << "child's
use_count() = " << child.use_count() << endl; return 0; }
  Another circular reference count :
closs node { public: typedef shared_ptr<Node> ptr_type; ptr_type next; };
auto p1 = make_shared_ptr<Node>(); auto p2 = make_shared_ptr<Node>();
weak Of lock function , To use within an action lock() Will return a shared_ptr.

This object can be manipulated weak_ptr Pointed to shared_ptr.

 
/// /// @file shared.cpp /// @author kogan(709954896@qq.com) /// @date
2020-06-21 11:51:39 /// #include <iostream> #include <memory> using std::cout;
using std::endl; int main() { // Initialization mode std::shared_ptr<int> ptr(new int(3));
std::shared_ptr<int> ptr2 = std::make_shared<int>(5); // take ptr2 Managed pointer assigned to ptr3
std::shared_ptr<int> ptr3(ptr2); cout << "reference count: " <<
ptr3.use_count() << endl; std::weak_ptr<int> wptr(ptr); cout << "reference
count: " << ptr3.use_count() << endl; { cout << "*ptr: " << *ptr << endl;
std::weak_ptr<int> wptr(ptr); auto sp = wptr.lock(); (*sp) = 5; cout << "*ptr:
" << *ptr << endl; } cout << "*ptr: " << *ptr << endl; return 0; }
 

Technology
©2019-2020 Toolsou All rights reserved,
about Navicat for mysql Of 2003 error unity Get the car speed and forward or backward PostgreSQL: Nine . Indexes Python realization switch method Keras Summary of training data loading ( Essence 2020 year 6 month 3 Daily update ) TypeScript Detailed explanation of Chinese interface mysql No backup recovery Three.js - OrbitControls Orbit control around target target parameter element ui Drop down box search function inherit jpa Repository Write custom method query