Here's a quote from cppreference's implementation note section of std::shared_ptr
, which mentions that there are two different pointers(as shown in bold) : the one that can be returned by get()
, and the one holding the actual data within the control block.
>In a typical implementation, `std::shared_ptr` holds only two pointers:
>
>1. **the stored pointer (one returned by `get()`)**
>2. a pointer to control block
>
>The control block is a dynamically-allocated object that holds:
>
>1. **either a pointer to the managed object or the managed object itself**
>2. the deleter (type-erased)
>3. the allocator (type-erased)
>4. the number of `shared_ptrs` that own the managed object
>5. the number of `weak_ptrs` that refer to the managed object
>
>The pointer held by the `shared_ptr` directly is the one returned by `get()`, while the pointer or object held by the control block is the one that will be deleted when the number of shared owners reaches zero. These pointers are not necessarily equal.
My question is, why is two different pointer needed for the object? doesn't the one returned by get()
suffice? and why aren't these pointers necessarily equal?
Aucun commentaire:
Enregistrer un commentaire