当前位置:网站首页>Multiple smart pointers
Multiple smart pointers
2022-07-01 22:59:00 【Hello, future】
- The function of smart pointer is to prevent memory leakage
- The phenomenon of using ordinary pointers : If malloc and free There are throwing exceptions between , namely malloc No execution after execution free A series of problems without releasing resources were introduced
- Should satisfy :1. Can automatically release memory -> Reduced memory leaks .2. It can also be used as a pointer
- Used in smart pointers RAII-> The compiler automatically calls functions to free memory —- It reminds us of the constructors and destructors that the compiler automatically calls in the class . Encapsulate the pointer into a class , Create resources in the structure , Release resources in decomposition
- use _CrtDumpMemoryLeaks() Function can detect memory leaks
- The realization principle of intelligent pointer :
- 1.RAII Achieve automatic release of resources ( It is to use a class to manage the resources on the heap , It uses the principle of compiler automatic invocation ), and string The difference between the pointers managed in , Smart pointer management is to manage the space on the heap of out of class applications .
- 2. Operations similar to pointers * and ->,( These two are simple ) Just overload these two operators
- 3. How to solve shallow copy , Cannot be solved by deep copy , and string Is the difference between the string Resources of are applied within the class , Smart pointers are pointers outside the management class , That's the difference , So it's not like string Same deep copy . Header file of smart pointer #include<memory>
- c++98 Provide auto_ptr. . Usage method ,auto_ptr<int>ptr(new int);auto_ptr Don't use . The method of copy construction is , Management of transferred resources , take p1 The managed resources are transferred to p2,p2 Management resources ,p1 Internal resources null. The reason for not using it is this p1 Out of commission , But programmers don't know , visit p1 There are problems
- c++98~11 Improvements between : The reason for not using shallow copies is to avoid releasing multiple times , no need auto_ptr Because there is a pointer that cannot be used . After improvement, both pointers can point to this space , Have the right to use , But only one pointer can free this space . Mark in the member variable whether this object can delete, Is this object has management power . This method is not recommended . Defects may cause wild pointers . Small in function {} In the scope, the object that has the right to release is destructed , Then the pointer pointing to this space at the back becomes the night pointer . Access will make an error .
- c++11 And use the method of resource transfer , repair 98 The pit of . Use unique_ptr Simple and crude solution to shallow copy , Resources are exclusive . Don't let copy and assignment , The implementation principle of no copy ,c++11:unique_ptr(const unique_ptr<T>&)=delete; The implementation of copy construction is to tell the compiler , Copy construction is not generated by the compiler .c++11 Expanded in delete The function of , Control the compiler to generate default member functions .c++98: By setting the declaration of copy construction to private member function , But the function body is not defined , The reason for giving it private is , Private, even if the function body is defined outside the class , I can't adjust it . Only give a statement because it is meaningless , Because the original purpose is not to want this function ... That's it
- Generally, continuous space is not used unique_ptr( Intelligent pointer ) management , Use it directly vector That's it
- unique_ptr The defect of is that multiple objects cannot share resources, that is, they cannot copy and assign values
- shared_ptr: Share smart pointers , How to solve shallow copy : Reference count , solve unique_ptr Problems that cannot be copied and . Only the reference count is 1 It's only when you destruct delete. also : Reference count cannot be set to staic Static member variable , Because static member variables are accessible to all objects ,, What is needed now is a count of the same resource . Therefore, the same resource should include an additional pointer to maintain a reference count ---> Thread safety should be considered . After thread safety is solved, there may be exceptions between locking and unlocking , Throw exceptions and exit directly , So it causes deadlock , So use class packaging lock ( Pay attention to the reference type of member variables when packaging locks , Otherwise, it will not be a lock ), Exit is the end of the function or out of the scope , Auto call destructor unlocked .... also share_ptr There are circular references , The result is resource leakage

- shared_ptr How to solve circular reference :weak_ptr, The only function is to assist shared_ptr Solve the problem of circular reference , He cannot manage resources independently .:::: The principle is , The underlying base class maintains two sets of application counts .use by 0 Is to release resources ,weak by 0 You can release the resources of reference counting
- Only smart pointers can assign and point to each other , Ordinary pointer and smart pointer cannot point to , For example, assignment , Is to copy one object to another , Ordinary pointer can't do this ,shared_pte Can also manage not new Out of the pointer , however weak_ptr Only manage others' resources
边栏推荐
- [image segmentation] 2021 segformer neurips
- cvpr2022 human pose estiamtion
- SAP GUI 里的收藏夹事务码管理工具
- 思科--高可用和高可靠网络考试
- Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
- Selection of all-optical technology in the park - Part 2
- Preparation of functional test report
- 若干互联网暴露面的收敛及处置建议
- cvpr2022 human pose estiamtion
- Armbain系统根分区空间不足处理
猜你喜欢

工作中非常重要的测试策略,你大概没注意过吧

Mysql5.7 set password policy (etc. three-level password transformation)

104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code

Reprint CSDN article operation

tcpdump命令使用详解

Turn -- the underlying debugging principle of GDB is so simple

Selection of all-optical technology in the park - Part 2

Cut noodles C language

【无标题】

MySQL -- index of MyISAM storage engine
随机推荐
Daily question brushing record (10)
Fiori applications are shared through the enhancement of adaptation project
Turn -- the underlying debugging principle of GDB is so simple
Digital currency: far-reaching innovation
第三方验收测试有什么好处?专业第三方软件测试机构推荐
工作中非常重要的测试策略,你大概没注意过吧
若干互联网暴露面的收敛及处置建议
Detailed explanation of common configurations in redis configuration file [easy to understand]
【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
pytorch训练自己网络后可视化特征图谱的代码
Reprint CSDN article operation
Preparation of functional test report
Single step debugging analysis of rxjs observable of operator
Kubernetes create service access pod
Unable to climb hill sort, directly insert sort
转载csdn文章操作
思科--高可用和高可靠网络考试
MySQL stored procedure
下班前几分钟,我弄清了v-model与.sync的区别
Rust language - Introduction to Xiaobai 05
