Generic pointers do not increment use_count

I am trying to figure out how to use std::shared_ptrin C ++. But this is rather confusing, and I donโ€™t understand how to create several common pointers pointing to the same object. Even the documentation and online materials are not very clear.

Below is a small snippet of code that I wrote to try to understand the behavior std::shared_ptr:

#include <iostream>
#include <memory>
using namespace std;

class Node
{
public:
    int key;
    Node()
    {
        key = 0;
    }
    Node(int k)
    {
        key = k;
    }
};

int main()
{
    Node node = Node(10);

    shared_ptr<Node> ptr1((shared_ptr<Node>)&node);
    cout << "Use Count: " << ptr1.use_count() << endl;

    // shared_ptr<Node> ptr2=make_shared<Node>(node);//This doesn't increase use_count
    shared_ptr<Node> ptr2((shared_ptr<Node>)&node);
    cout << "Use Count: " << ptr2.use_count() << endl;

    if (ptr1 == ptr2)
        cout << "ptr1 & ptr2 point to the same object!" << endl;

    if (ptr1.get() == ptr2.get())
        cout << "ptr1 & ptr2 point to the same address!" << endl;

    cout << "ptr1: " << ptr1 << "  "
         << "ptr2: " << ptr2 << endl;
    return 0;
}

Based on this result, both ptr1and ptr2point to the same object Node, but use_countis 1 for both of them. Even when used, it std::make_shareddoes not work, and the program crashes before exiting.

Could you tell me what I am doing wrong? And how to create multiple shared_ptr(s) that point to the same object.

+4
3

Use_count shared_ptr ( make_shared), . shared_ptr shared_ptr , ( , ).

, shared_ptr ; use_count shared_ptr shared_ptr. .

shared_ptr<Node> ptr1 = make_shared<Node>(10);
cout<<"Use Count: "<<ptr1.use_count()<<endl; // 1

shared_ptr<Node> ptr2(ptr1);
cout<<"Use Count: "<<ptr2.use_count()<<endl; // 2
cout<<"Use Count: "<<ptr1.use_count()<<endl; // 2

BTW: , shared_ptr &node, , . .

Node* node = new Node(10);

shared_ptr<Node> ptr1(node);
cout<<"Use Count: "<<ptr1.use_count()<<endl; // 1

shared_ptr<Node> ptr2(ptr1);
cout<<"Use Count: "<<ptr2.use_count()<<endl; // 2
cout<<"Use Count: "<<ptr1.use_count()<<endl; // 2
+6

shared_ptr<> , . , shared_ptr . shared_ptr, new make_shared:

shared_ptr<Node> ptr1(new Node(42));
shared_ptr<Node> ptr2 = make_shared<Node>();
shared_ptr<Node> ptr3 = make_shared<Node>(99);

, use_count()==1. , :

shared_ptr<Node> copy1 = ptr1;

ptr1 copy1 use_count()==2.

+3

, : ++ , . , , , , , . . :

int bla()
{
    int a = 1;
    int b = 2;
    int result = a+b;
    return result;
}

3 int , bla , .

. new. , , delete. memoryleaks, (, shared_ptr, ) . , shared_ptr ( (!) (, , , )), shared_ptr . , , shared_ptr . shared_ptr. shared_ptr , , , . , shared_ptr , , . , , shared_ptr.

, :

  • Node node = Node(10); (, ). . shared_ptr, node. . , shared_ptr node ( ) node. .

  • , 1 . : , shared_ptr . shared_ptr , node. .

, , node shared_ptr, shared_ptr , . , 2 :

std::shared_ptr<int> mysharedpointer (new int(ANYNUMBER));
auto mysharedpointer = std::make_shared<int>(ANYNUMBER);

, .

shared_ptr, , , :

auto mysecondsharedpointer = mysharedpointer;

voila , .

( , , - . )

+2

Source: https://habr.com/ru/post/1692082/


All Articles