網站首頁 編程語言 正文
一、提要
boost::shared_ptr是另一個智能指針,與 boost::scoped_ptr有很大不同,本文闡述這種區別。
二、智能指針boost::shared_ptr與boost::scoped_ptr
主要區別在于:
- boost::shared_ptr 不一定是對象的獨占所有者。
- 所有權可以與 boost::shared_ptr 類型的其他智能指針共享。在這種情況下,共享對象不會被釋放,直到引用該對象的共享指針的最后一個副本被銷毀。
- 因為 boost::shared_ptr 可以共享所有權,所以可以復制智能指針,而這對于 boost::scoped_ptr 是不可能的。
boost::shared_ptr
定義在boost/shared_ptr.hpp
.
三、智能指針 boost::shared_ptr用法
示例1
基本調用boost::shared_ptr
示例 1
#include <boost/shared_ptr.hpp> #include <iostream> int main() { boost::shared_ptr<int> p1{new int{1}}; std::cout << *p1 << '\n'; boost::shared_ptr<int> p2{p1}; p1.reset(new int{2}); std::cout << *p1.get() << '\n'; p1.reset(); std::cout << std::boolalpha << static_cast<bool>(p2) << '\n'; }
示例 1 使用了 boost::shared_ptr 類型的兩個智能指針 p1 和 p2。 p2 用 p1 初始化,這意味著兩個智能指針共享同一個 int 對象的所有權。當在 p1 上調用 reset() 時,一個新的 int 對象被錨定在 p1 中。這并不意味著現有的 int 對象被銷毀。由于它也錨定在 p2 中,因此它繼續存在。在調用 reset() 之后,p1 是編號為 2 的 int 對象的唯一所有者,而 p2 是編號為 1 的 int 對象的唯一所有者。
boost::shared_ptr 在內部使用引用計數器。只有當 boost::shared_ptr 檢測到智能指針的最后一個副本已被銷毀時,才會使用 delete 釋放包含的對象。
與 boost::scoped_ptr 一樣,boost::shared_ptr 重載 operator bool()、operator*() 和 operator->()。成員函數 get() 和 reset() 用于檢索當前存儲的地址或存儲新地址。
作為第二個參數,可以將刪除器傳遞給 boost::shared_ptr 的構造函數。刪除器必須是一個函數或函數對象,它接受實例化時使用的 boost::shared_ptr 類型的指針作為其唯一參數。在析構函數中調用刪除器而不是刪除。這使得管理 boost::shared_ptr 中動態分配的對象以外的資源成為可能。
示例2
boost::shared_ptr
用戶自定義刪除
示例 2
#include <boost/shared_ptr.hpp> #include <Windows.h> int main() { boost::shared_ptr<void> handle(OpenProcess(PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId()), CloseHandle); }
在示例2 boost::shared_ptr 被實例化為 void。傳遞給構造函數的第一個參數是 OpenProcess() 的返回值。 OpenProcess() 是一個用于獲取進程句柄的 Windows 函數。在示例中,OpenProcess() 返回當前進程的句柄 - 示例本身。
Windows 使用句柄來引用資源。一旦不再使用資源,必須使用 CloseHandle() 關閉句柄。 CloseHandle() 期望的唯一參數是要關閉的句柄。在示例中,CloseHandle() 作為第二個參數傳遞給 boost::shared_ptr 的構造函數。 CloseHandle() 是句柄的刪除器。當在 main() 結束時銷毀句柄時,析構函數調用 CloseHandle() 以關閉作為第一個參數傳遞給構造函數的句柄。
示例2 之所以有效,是因為 Windows 句柄被定義為 void*。如果 OpenProcess() 沒有返回 void* 類型的值,并且如果 CloseHandle() 沒有預期 void* 類型的參數,則無法在此示例中使用 boost::shared_ptr。刪除器不會使 boost::shared_ptr 成為管理任意資源的靈丹妙藥。
示例3
用boost::make_shared
#include <boost/make_shared.hpp> #include <typeinfo> #include <iostream> int main() { auto p1 = boost::make_shared<int>(1); std::cout << typeid(p1).name() << '\n'; auto p2 = boost::make_shared<int[]>(10); std::cout << typeid(p2).name() << '\n'; }
Boost.SmartPointers 在 boost/make_shared.hpp 中提供了一個輔助函數 boost::make_shared()。使用 boost::make_shared(),您可以創建 boost::shared_ptr 類型的智能指針,而無需自己調用 boost::shared_ptr 的構造函數。
boost::make_shared() 的優點是必須動態分配的對象內存和智能指針內部使用的引用計數器的內存可以保留在一個塊中。使用 boost::make_shared() 比調用 new 來創建動態分配的對象并在 boost::shared_ptr 的構造函數中再次調用 new 來為引用計數器分配內存更有效。
您也可以對數組使用 boost::make_shared()。在示例 1.5 中第二次調用 boost::make_shared() 時,一個具有十個元素的 int 數組被錨定在 p2 中。
boost::shared_ptr 自 Boost 1.53.0 起僅支持數組。 boost::shared_array 提供了一個類似于 boost::shared_ptr 的智能指針,就像 boost::scoped_array 類似于 boost::scoped_ptr 一樣。當使用 Visual C++ 2013 和 Boost 1.53.0 或更高版本構建時,示例 1.5 為 p2 打印 class boost::shared_ptr<int [0]>。
從 Boost 1.53.0 開始,boost::shared_ptr 支持單個對象和數組,并檢測是否必須使用 delete 或 delete[] 釋放資源。因為 boost::shared_ptr 還重載了 operator[](自 Boost 1.53.0 起),所以這個智能指針是 boost::shared_array 的替代方案。
示例4
用boost::shared_array
Example 4 .Using
#include <boost/shared_array.hpp> #include <iostream> int main() { boost::shared_array<int> p1{new int[1]}; { boost::shared_array<int> p2{p1}; p2[0] = 1; } std::cout << p1[0] << '\n'; }
boost::shared_array 補充 boost::shared_ptr:由于 boost::shared_array 在析構函數中調用 delete[],所以這個智能指針可以用于數組。對于早于 Boost 1.53.0 的版本,boost::shared_array 必須用于數組,因為 boost::shared_ptr 不支持數組。
boost::shared_array 在 boost/shared_array.hpp 中定義。
在示例4 中,智能指針 p1 和 p2 共享動態分配的 int 數組的所有權。當使用 operator[] 訪問 p2 中的數組以存儲數字 1 時,使用 p1 訪問相同的數組。因此,該示例將 1 寫入標準輸出。
與 boost::shared_ptr 一樣,boost::shared_array 使用引用計數器。當 p2 被銷毀時,動態分配的數組不會被釋放,因為 p1 仍然包含對該數組的引用。只有當 p1 的作用域結束時,該數組才會在 main() 結束時被銷毀。
boost::shared_array 還提供了成員函數 get() 和 reset()。此外,它使運算符 operator bool 過載。
示例5
用boost::shared_ptr
和BOOST_SP_USE_QUICK_ALLOCATOR
Example5.boost::shared_ptr
和BOOST_SP_USE_QUICK_ALLOCATOR
#define BOOST_SP_USE_QUICK_ALLOCATOR #include <boost/shared_ptr.hpp> #include <iostream> #include <ctime> int main() { boost::shared_ptr<int> p; std::time_t then = std::time(nullptr); for (int i = 0; i < 1000000; ++i) p.reset(new int{i}); std::time_t now = std::time(nullptr); std::cout << now - then << '\n'; }
選擇像 boost::shared_ptr 這樣的智能指針而不是標準庫中的智能指針是有意義的。 Boost.SmartPointers 支持宏來優化智能指針的行為。示例 5 使用宏 BOOST_SP_USE_QUICK_ALLOCATOR 來激活 Boost.SmartPointers 附帶的分配器。此分配器管理內存塊以減少對引用計數器的 new 和 delete 調用次數。該示例調用 std::time() 來測量循環前后的時間。雖然執行循環所需的時間取決于計算機,但使用BOOST_SP_USE_QUICK_ALLOCATOR 的示例可能會比不使用時運行得更快。 Boost.SmartPointers 的文檔沒有提到 BOOST_SP_USE_QUICK_ALLOCATOR。因此,您應該分析您的程序并比較使用和不使用 BOOST_SP_USE_QUICK_ALLOCATOR 獲得的結果。
Chapter1.Boost.SmartPointers - Shared Ownership (theboostcpplibraries.com)
原文鏈接:https://yamagota.blog.csdn.net/article/details/127045943
相關推薦
- 2023-03-11 Go語言中init函數與匿名函數使用淺析_Golang
- 2022-10-25 在PyCharm中使用FMEObjects的操作步驟_python
- 2022-09-03 python中*args與**kwarsg及閉包和裝飾器的用法_python
- 2023-02-01 C++泛型編程綜合講解_C 語言
- 2022-07-15 Python?print函數:如何將對象打印輸出_python
- 2022-11-10 Android獲取設備傳感器的方法_Android
- 2022-07-13 CSS 不需要清除浮動的圣杯布局~面試可能會問
- 2022-11-27 Python?OpenCV實現圖像增強操作詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支