当前位置:网站首页>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
边栏推荐
猜你喜欢

转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程

Metauniverse may become a new direction of Internet development

Understanding of indexes in MySQL

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received

Cut noodles C language

Favorite transaction code management tool in SAP GUI

今日睡眠质量记录71分

Chen Tianqi's machine learning compilation course (free)

"Trust machine" empowers development

好友新书发布,祝贺(送福利)
随机推荐
There is no signal in HDMI in computer games caused by memory, so it crashes
[JUC learning road day 8] condition
cvpr2022 human pose estiamtion
Happy number [fast and slow pointer of ring PROBLEMS]
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
Understanding of indexes in MySQL
ECMAScript 2022 was officially released. Have you heard about it?
Ffmpeg learning notes
locust的使用
思科--WAN 的概念考试外部工具
Fiori applications are shared through the enhancement of adaptation project
聊一聊Zabbix都监控哪些参数
[image segmentation] 2021 segformer neurips
Hide the creation and use of users
Unable to climb hill sort, directly insert sort
Map container
工作中非常重要的测试策略,你大概没注意过吧
Origin2018安装教程「建议收藏」
Cisco exam -- redundant network
Kubernetes create service access pod
