What happens when a copy of shared_ptr is created?

I want to understand how the reference score of a managed entity affects shared_ptr

when shared_ptr

assigned to another.

I came across the following statement in C ++ primer, 5th edition , which:

For example, the counter associated with a shared_ptr is incremented when ... we use it as the right assignment operand . The counter is decremented when we assign a new value to the shared_ptr ...

It is shown as an example:

auto p = make_shared<int>(42); // object to which p points has one user

auto q(p); // p and q point to the same object
           // object to which p and q point has two users

auto r = make_shared<int>(42); // int to which r points has one user
r = q; // assign to r, making it point to a different address
       // increase the use count for the object to which q points
       // reduce the use count of the object to which r had pointed
       // the object r had pointed to has no users; that object is automatically freed

      

When I run similar code, this is not my observation:

code:

#include<iostream>
#include<memory>

int main()
{
  std::shared_ptr<int> sh1 = std::make_shared<int>(1);
  std::shared_ptr<int> sh2 = std::make_shared<int>(2);

  sh2 = sh1;

  std::cout << "sh1 use count: " << sh1.use_count() << std::endl;
  std::cout << "sh2 use count: " << sh2.use_count() << std::endl;

  return 0;
}

      

Output

sh1 using count: 2

Use counter sh2: 2

How can use_count

of sh2

also 2? Shouldn't it be 0 according to the above text? Did I miss something?

+3


source to share


3 answers


First you had sh1.use_count=1

and sh2.use_count=1

. Now when you assign use sh2=sh1

this happens:



  • The counter is sh2

    decremented by one because sh2

    ( shared_ptr

    ) will accept another pointer
  • Since sh2.use_count=0

    now the object is under its pointer, which int(2)

    is destroyed.
  • Now you have assigned a sh2

    new object that belongs to sh1

    , and therefore its counter is incremented by one, so: sh2.use_count=2

    and of course also sh1.use_count=2

    , since both objects shared_ptr

    point to the same object that is int(1)

    .
+9


source


Initially we have

sh1.use_count =1 and 
sh2.use_count = 1
*sh1  = 1 and 
*sh2  = 2

      

After sh2 = sh1

. Both sh1 ->int(1) <- sh2

.

What's up with int(2)

? It was destroyed as it sh2

now points to int(1)

.



Therefore, we have

sh1.use_count == sh2.use_count
*sh1 == *sh2

      

* sh1 and * sh2 have 1

0


source


I think this misunderstanding is due to the fact that you think the counter is stored in the instance shared_ptr

along with a pointer to the object it owns. However, the instance actually shared_ptr

only contains a pointer to an internal storage object containing both a watchdog counter and a pointer to its owned object. So when you make an assignment sh2 = sh1;

, you are making a sh2

reference to the same internal storage object as and sh1

so the value of the counters reported by both sh1

and sh2

are taken from the same source.

0


source







All Articles