当前位置:网站首页>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
边栏推荐
- Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
- 元宇宙可能成为互联网发展的新方向
- Pytorch nn.functional.unfold()的简单理解与用法
- "Trust machine" empowers development
- 业务可视化-让你的流程图'Run'起来
- The principle, testing and Countermeasures of malicious software reverse closing EDR
- Chen Tianqi's machine learning compilation course (free)
- 台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
- Selection of all-optical technology in the park - Part 2
- internal field separator
猜你喜欢
随机推荐
Metauniverse may become a new direction of Internet development
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
MySQL -- index of InnoDB storage engine
Explain the use of locksupport in detail
Fiori 应用通过 Adaptation Project 的增强方式分享
[QT widget] encapsulates a simple thread management class
旅游管理系统
[JUC learning road day 9] barrier derivatives
Share some feelings of a programmer who has experienced layoffs twice a year
工作中非常重要的测试策略,你大概没注意过吧
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
Daily question brushing record (10)
使用3DMax制作一个象棋棋子
nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
[literacy] deep / shallow, local / global features in machine learning image processing
Arlo's thinking after confusion
Data enhancement of semi supervised learning
想请教一下,证券开户选择哪个证券比较好?手机开户是安全么?
leetcode - 287. Find duplicates
Little red book scheme jumps to the specified page