Bifalgorithm post

Smart Pointers and C++. Unique Pointer. Unique Pointer will allocate memory and assign it one owner. If you try to assign this unique pointer to another variable, you will have a compiler error. However, if you want to assign a new owner, you have to use standard move. You can also pass a reference to the actual underlying data. You can do this by dereferencing the actual unique pointer and passing the variable into a function parameter, which accepts a reference. Shared pointer. Shared pointer is a pointer that will have multiple owners. We can actually assign the initial shared pointer into different variables unlike the unique pointer. And if we access the original pointer and its use count variable, we see how many total owners we have. Weak pointer. Like before, we said that if you assign a shared pointer to a new variable, the number of owners will increase. However, a weak pointer doesn't actually own the data. It only kind of sits there and views the data. This is clear when we check the use count variable for our initial shared pointer. Weak pointers are very useful for resolving cyclical dependencies. Despite it not being an actual owner, if you want to access the data, we have to lock the weak pointer. By locking it, we get the shared pointer back. This will increase the count of the initial shared pointer, which where we constructed the weak pointer from. And you can see this inside this code where inside the if block, the use count goes up to two and outside the if block since it has exited the if block, the owner is dropped and we will return back to one owner. Smart pointers in C++ can make memory management very easy. However, they can be confusing to a lot of newcomers and knowing which one to use is very important.

Smart Pointers and C++. Unique Pointer. Unique Pointer will allocate memory and assign it one owner. If you try to assign this unique pointer to another variab…

Ideas in this flowchart

  • Smart Pointers in C++
  • Unique Pointer
  • Memory Allocation
  • Single Owner
  • Compiler Error on Assignment
  • Transfer Ownership with std::move
  • Pass Reference
  • Dereference and Pass
  • Shared Pointer
  • Multiple Owners
  • Assign to Different Variables
  • Use Count
  • Weak Pointer
  • Doesn't Own Data
  • Views Data
  • Resolve Cyclical Dependencies
  • Lock to Access Data
  • Increase Use Count
  • Memory Management
  • Ease of Use
  • Confusion for Newcomers
  • Importance of Correct Usage
Feed/

Smart Pointers and C++. Unique Pointer. Unique Pointer will allocate memory and assign it one owner. If you try to assign this unique pointer to another variable, you will have a compiler error. However, if you want to assign a new owner, you have to use standard move. You can also pass a reference to the actual underlying data. You can do this by dereferencing the actual unique pointer and passing the variable into a function parameter, which accepts a reference. Shared pointer. Shared pointer is a pointer that will have multiple owners. We can actually assign the initial shared pointer into different variables unlike the unique pointer. And if we access the original pointer and its use count variable, we see how many total owners we have. Weak pointer. Like before, we said that if you assign a shared pointer to a new variable, the number of owners will increase. However, a weak pointer doesn't actually own the data. It only kind of sits there and views the data. This is clear when we check the use count variable for our initial shared pointer. Weak pointers are very useful for resolving cyclical dependencies. Despite it not being an actual owner, if you want to access the data, we have to lock the weak pointer. By locking it, we get the shared pointer back. This will increase the count of the initial shared pointer, which where we constructed the weak pointer from. And you can see this inside this code where inside the if block, the use count goes up to two and outside the if block since it has exited the if block, the owner is dropped and we will return back to one owner. Smart pointers in C++ can make memory management very easy. However, they can be confusing to a lot of newcomers and knowing which one to use is very important.

"Smart Pointers and C++. Unique Pointer."

👍
0
0 comments0 shares

Sign in to view and post comments.