Corn
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 【C++】Smart Pointer 智慧指標筆記 ## Smart Pointers 在 `C++11` 中的標準模板庫 ( STL,為了要解決**記憶體洩漏問題 ( Memory Leak )** 的問題,便引進了 Smart Pointer 來更方便管理記憶體,而標頭檔案為 `<memory>` > Smart Pointers enable automatic, exception-safe, object lifetime management. > [cppreference](https://en.cppreference.com/w/cpp/memory) :::danger 如果在 `free` 跟 `delete` 前程式碼就因為 Error 而結束,造成記憶體沒有釋放,對於系統穩定性造成一定程度的危害 ::: 嚴格上來說智慧指標是由**模板類別**來進行實作,可以參考 > [unique_ptr.h](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/unique_ptr.h#L271) > [shared_ptr.h](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr.h#L534) ![image](https://hackmd.io/_uploads/r1mRmsCsA.png =50%x) 👉[ C 語言程式記憶體配置概念](/byDxZ7bCRAaCTDUYTjP66w) ## `std::unique_ptr` `unique_ptr` 是一種smart pointer,並且確保 `unique_ptr` 的所指向的物件只會被一個智慧指標進行管理,當超出作用域時變自動進行銷毀 ### Declare 宣告 `unique_ptr` 有以下兩種方法 * 在 `C++11` ,使用 `std::unique_ptr<T> ptr(new T(...));` * 在 `C++14` ,使用 `std::make_unique<T>(Args&& ... args)` ```cpp! class A{ public: A(){std::cout << "A created" << std::endl;} ~A(){ std::cout << "A destroyed" << std::endl;} }; int main() { std::cout << "Example 1" << std::endl; { std::unique_ptr<A> aInst1(new A()); // C++11 std::unique_ptr<A> aInst2 = std::make_unique<A>(); // C++14 } std::cout << "Example 1 out of scope" << std::endl; return 0; } ``` * 以下例子為嘗試將 `unique_ptr` 進行賦值動作,在編譯時期就會出錯! 原因是因為在其模板函數的實作中,已經將賦值的 operator 禁用了 * 如果需要轉移 `unique_ptr` 的 ownership,就需要使用`std::move`,移動完成後本來的`aInst`就會是空指標 ```cpp std::unique_ptr<A> otherA; std::unique_ptr<A> aInst = std::make_unique<A>(); // C++14 otherA = aInst; // error: use of deleted function otherA = std::move(aInst); // success ``` ## `std::shared_ptr` `shared_ptr` 是一種可以共享同一個資源的智慧指標,內部會記錄這份資源被使用的次數 (Reference Counter) ,只要還有 `shared_ptr` 指向的物件存在時、資源就不會釋放;只有當所有使用這份資源的 shared_ptr 物件都消失的時候,資源才會被自動釋放 * 初始化 Reference Counter設為 1 * 拷貝或附值 Reference Counter會加 1 * Reset Reference Counter設為 0 ### Declare * 在 `C++11` ,使用 `std::shared_ptr<T> ptr(new T(...));` * 在 `C++14` ,使用 `std::make_shared<T>(Args&& ... args)` ```cpp! #include <iostream> #include <memory> #define watch(shPtr) std::cout << "get: " << shPtr.get() << " use_count: " << shPtr.use_count() << std::endl class A { public: A(){std::cout << "A created" << std::endl;} ~A(){ std::cout << "A destroyed" << std::endl;} }; int main() { std::shared_ptr<A> a = std::make_shared<A>(); watch(a); std::shared_ptr<A> otherA = a; watch(a); watch(otherA); return 0; } ``` ## `std::weak_ptr` 搭配 `shared_ptr` 使用的smart pointer,和 `shared_ptr` 的不同點在於 `weak_ptr` 不會影響資源被使用的次數,也就是說的 `weak_ptr` 存在與否不代表資源會不會被釋放掉,而 `weak_ptr` 只是用來檢測記憶體是否正確被釋放 ![smartpoint](https://hackmd.io/_uploads/H1xdAZtf0.png =60%x) ## Smart Pointer Operator * `a->b` = `(*a).b` 用於取得智慧指標的屬性或方法 * `(*ptr)` and `ptr.get()` 取得智慧指標中的**指向物件的原生指標**,回傳 reference ,不影響智慧指標,使用場景通常為將指向物件只有要被使用,但不改變內部狀態時候傳入使用,避免造成循環引用造成記憶體洩漏 ## Ownership 擁有權 藉由動態配置 ( 例如通過 `new` 或 `std::make_unique` 創建的物件 ) 的物件或函數都會由一個管理者,負責在物件不再需要的時候將物件刪除,而所有權是可以共享的,也就是可以將物件或函數傳遞到下一個管理者,而最後一個管理者要負責將物件刪除,也就是物件或函數創建與刪除,是有可能在不同管理者之間執行的,造成管理記憶體的難處 ## 動態分配物件的傳遞方式 ### 傳遞引用或指標 分配物件的程式碼(例如一個類別)負責管理物件的生命週期,其他程式碼只「借用」該物件,因此我們可以透過引用方式來「借用」該物件,分為 `const Type&` 與 `Type&` 或者 `const Type*` 與 `Type*`,優點就是避免不必要的複製開銷、同時管理權限分明,誰創建誰就負責銷毀,借用者沒有資格去銷毀該借用物件,上面有看到有時候用常數引用或者引用,則是端看借用者是否需要修改該物件狀態,如果有則不能使用常數引用或指標 :::info * 引用無法使用空指標,如果該物件有可能不存在,請用指標 * 請確保借用的程式碼所使用物件期間,該物件都存在,不會突然被銷毀 ::: ### 傳遞副本 基本上小型物件或者配置檔案,都可以用複製的,或者該類別需要長期持有該物件 ## 結論 如果需要動態配置的話,儘可能把所有權保持在配置記憶體的那段程式碼中。如果其他地方也需要存取該物件的話,考慮複製一份物件,或是傳遞物件的指標 (Raw Pointer) 或 reference `&` 方式,不要傳遞所有權。如要真的要轉移所有權 (Ownership) 最好使用 `std::unique_ptr` 明確表達所有權的轉移,至於是否要複製或者傳遞原指標則需要考量多個因素進而影響實作 ## 以狀態設計模式為例 以狀態設計模式為例,我們可以透過 `Context` 管理各個狀態所需要的資源,並藉由原始指標 (Raw pointer) 方式傳入到 `State` 當中,讓各個從 `State` 衍生出的類別,可以使用這些資源,這樣便提供了類似一個工具包的角色,並讓不同狀態去取用,同時也可以**傳遞狀態間的資源或資料轉移** ```cpp! class State { protected: Context* context_ = nullptr; public: void setContext(Context* ctx) { context_ = ctx; } }; ``` ```cpp! class Context { public: Context(); ~Context(); private: std::shared_ptr<Resource> resource_; // Shared resource }; ``` ## 補充知識點 1. 作用域Scope: 程式碼有效的區域,例如 python 的縮排 The scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully