Table of Contents
- 1 In what situation is a Shared_ptr more appropriate than a Unique_ptr?
- 2 What is the difference between a raw pointer and a smart pointer?
- 3 When should you not use smart pointers?
- 4 When should Shared_ptr be used?
- 5 Is Shared_ptr assignment Atomic?
- 6 Do I need to delete Shared_ptr?
- 7 When to use intrusive_PTR vs shared_ptr?
- 8 When should I use unique_ptr<> instead of references?
Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.
What is the difference between a raw pointer and a smart pointer?
A smart pointer is a class that wraps a ‘raw’ (or ‘bare’) C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a ‘raw’ pointer in a practical way. Smart pointers should be preferred over ‘raw’ pointers.
Do linked lists use pointers?
A linked list is a list constructed using pointers. A linked list is not fixed in size but can grow and shrink while your program is running.
Is Shared_ptr thread safe?
A std::shared_ptr consists of a control block and its resource. Yes, the control block is thread-safe; but no, the access to the resource is not thread-safe. That means, modifying the reference counter is an atomic operation and you have the guarantee that the resource will be deleted exactly once.
When should you not use smart pointers?
You should not use smart pointers when you want to pass a reference to an object to a function and the function does not destroy or prevents the destruction of the object. In other words, if the function does not participate in the lifecycle of the passed object.
So, we should use shared_ptr when we want to assign one raw pointer to multiple owners. // referring to the same managed object. When to use shared_ptr? Use shared_ptr if you want to share ownership of a resource.
Is it important to learn linked list?
Linked lists offer some important advantages over other linear data structures. Unlike arrays, they are a dynamic data structure, resizable at run-time. Also, the insertion and deletion operations are efficient and easily implemented. Unlike arrays, linked lists aren’t fast at finding the n th n^\text{th} nth item.
When should we use Shared_ptr?
Use shared_ptr if you want to share ownership of a resource. Many shared_ptr can point to a single resource. shared_ptr maintains reference count for this propose. when all shared_ptr’s pointing to resource goes out of scope the resource is destroyed.
(the key part is the plural here) Standards shared_ptr use atomic inc/dec and cmp/xch in the release() method to check against 0 before deleting. This is not thread safe due to the 2nd ref count (the weak ref count). A weak ref could turn shared after the test has passed, and you have a dangling.
So no, you shouldn’t. The purpose of shared_ptr is to manage an object that no one “person” has the right or responsibility to delete, because there could be others sharing ownership. So you shouldn’t ever want to, either.
What is the difference between shared_ptr and raw pointer?
To use raw pointer (T*) or smarter pointer depends on who owns the object (whose responsibility to release memory of the obj). 5) To my undetstanding, shared_ptr is usually used when you don’t know who will release the obj, for example, one obj is used by multi-thread
Is it safe to pass a shared_ptr by reference?
It just has to access the pointer within the lifetime of the caller’s shared_ptr. In this case, it’s safe to pass the shared_ptr by reference, or pass the raw pointer or a reference to the underlying object. Passing this way provides a small performance benefit, and may also help you express your programming intent.
Use shared_ptr or intrusive_ptr when you want shared ownership of the pointer. This can be confusing and inefficient, and is often not a good option. Shared ownership can be useful in some complex designs, but should be avoided in general, because it leads to code which is hard to understand.
When should I use unique_ptr<> instead of references?
When there is no transfer of or shared ownership references or plain pointers are good enough. (Plain pointers are more flexible than references.) When there is transfer of ownership but no shared ownership then std::unique_ptr<> is a good choice. Often the case with factory functions.
https://www.youtube.com/watch?v=-dREJCf2ve4