網(wǎng)站首頁 編程語言 正文
1.為什么需要unique_ptr
與shared_ptr作用類似,需要解決內(nèi)存泄漏的問題,但是卻不需要使用shared_ptr的引用計數(shù),所以為了減少消耗,就需要一個這樣的智能指針。但是使用已被廢棄的auto_ptr的話就會有新的問題,auto_ptr在使用過程中如果被拷貝構(gòu)造或者賦值的話,被復(fù)制的auto_ptr就失去了作用,這個時候就需要在auto_ptr的基礎(chǔ)上禁用拷貝構(gòu)造以及賦值操作,也就成了unique_ptr。
2.什么是unique_ptr
一個unique_ptr獨享它指向的對象。也就是說,同時只有一個unique_ptr指向同一個對象,當(dāng)這個unique_ptr被銷毀時,指向的對象也隨即被銷毀。使用unique_ptr需要引入<memory.h>
3.unique_ptr特性
unique_ptr禁用了拷貝構(gòu)造以及賦值操作,也就導(dǎo)致了下面的這些操作無法完成。
void testFunction(std::unique_ptr<Test> t){ t->getString(); } void features(){ // Disable copy from lvalue. // unique_ptr(const unique_ptr&) = delete; // unique_ptr& operator=(const unique_ptr&) = delete; //不能進行拷貝構(gòu)造以及賦值運算,也就表示不能作為函數(shù)參數(shù)傳遞 std::unique_ptr<Test> t(new Test); std::unique_ptr<Test> t2 = t; //編譯報錯 std::unique_ptr<Test> t3(t);//編譯報錯 testFunction(t);//編譯報錯 }
4.如何使用unique_ptr
4.1簡單使用
void simpleUse(){ Test *test = new Test; std::unique_ptr<Test> t(test); qDebug() << test <<"獲取原始指針"<< t.get() <<endl; // t.release(); //釋放其關(guān)聯(lián)的原始指針的所有權(quán),并返回原始指針,沒有釋放對象 // t.reset();// 釋放對象 t->getString(); std::unique_ptr<Test> t2 = std::move(t); //交換使用權(quán)到t2; t2->getString(); }
4.2指向數(shù)組
和shared_ptr需要注意的地方一樣,指向數(shù)組時要注意模板書寫的方式,以及如何使用自定義刪除器
錯誤寫法:會導(dǎo)致內(nèi)存泄露
void customRemover(){ std::unique_ptr<Test> t(new Test[5]); }
正確寫法:
void customRemover(){ std::unique_ptr<Test[]> t(new Test[5]); std::unique_ptr<Test, void(*)(Test *)> p2(new Test[5],[](Test *t){ delete []t; }); }
5.unique_ptr需要注意什么
不要多個unique_ptr指向同一個對象
例如:
void repeatPointsTo(){ Test *test = new Test; std::unique_ptr<Test> t(test); std::unique_ptr<Test> t2(test); //兩個unique_ptrzhi'xi指向同一個對象,會導(dǎo)致這個對象被析構(gòu)兩次,導(dǎo)致問題出現(xiàn) }
會導(dǎo)致對象會被多次析構(gòu),導(dǎo)致崩潰
原文鏈接:https://blog.csdn.net/aaaaaacc7/article/details/122149762
相關(guān)推薦
- 2022-09-17 Python中re.findall()用法詳解_python
- 2022-07-09 Nginx利用Logrotate實現(xiàn)日志分割的詳細(xì)過程_nginx
- 2022-09-28 基于OpenCV(python)的實現(xiàn)文本分割之垂直投影法_python
- 2024-02-28 UNI-APP,動態(tài)設(shè)置view的背景圖片
- 2023-07-31 element中el-input無法輸入
- 2023-04-01 Pytorch基礎(chǔ)之torch.randperm的使用_python
- 2022-07-27 Python中的協(xié)程(Coroutine)操作模塊(greenlet、gevent)_python
- 2022-04-19 在html中src和href的區(qū)別,以及img中的srcset的作用是什么?
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支