raw指针和weak_ptr之间有什么区别?

时间:2021-08-16 13:29:50

As in title. This question probably already has an answer but I failed to find one.

如标题。这个问题可能已经有了答案,但我找不到答案。

2 个解决方案

#1


19  

The fundamental conceptual difference between a naked pointer and a weak_ptr is that if the object pointed to is destroyed, the naked pointer won't tell you about it. This is called a dangling pointer: a pointer to an object that doesn't exist. They're generally hard to track down.

裸指针和weak_ptr之间的基本概念差异在于,如果指向的对象被破坏,裸指针将不会告诉您它。这称为悬空指针:指向不存在的对象的指针。他们通常很难追查。

The weak_ptr will. In order to use a weak_ptr, you must first convert it into a shared_ptr. And if that shared_ptr doesn't point to anything, then the object was deleted.

weak_ptr会。要使用weak_ptr,必须先将其转换为shared_ptr。如果shared_ptr没有指向任何内容,那么该对象就被删除了。

For example:

#include <iostream>
#include <memory>

std::weak_ptr<int> wp;

void test()
{
    auto spt = wp.lock(); // Has to be copied into a shared_ptr before usage
    if (spt) {
        std::cout << *spt << "\n";
    } else {
        std::cout << "wp is expired\n";
    }
}

int main()
{
    {
        auto sp = std::make_shared<int>(42);
        wp = sp;
        test();
    }
    test();
}

Output

42
wp is expired

#2


18  

A raw pointer is (at least normally) simply an address. You can't tell anything about what it points at from the pointer itself.

原始指针(至少通常)只是一个地址。你无法从指针本身告诉它指出什么。

A weak_ptr is always associated with a shared_ptr, so we probably need to start with a shared_ptr to make any sense of a weak_ptr.

weak_ptr总是与shared_ptr相关联,因此我们可能需要从shared_ptr开始才能理解weak_ptr。

A shared_ptr is reference counted, so it keeps track of how many references (pointers) to an object exist, and automatically destroys the object when no more references to that object exist.

shared_ptr是引用计数,因此它跟踪对象存在多少引用(指针),并在不再存在对该对象的引用时自动销毁该对象。

As I already said, a weak_ptr is associated with a shared_ptr. Unlike a shared_ptr, the existence of a weak_ptr does not increment the reference count for the pointee object. To use a weak_ptr, you must first convert it to a shared_ptr. If the current reference count is positive, that will succeed, and converting the weak_ptr to a shared_ptr will increment the reference count to signify that the converted pointer is a "real" reference to the object. If, on the other hand, the reference count is already zero (meaning the pointee object has already been destroyed) the attempt to convert the weak_ptr to a shared_ptr will simply fail.

正如我已经说过的,weak_ptr与shared_ptr相关联。与shared_ptr不同,weak_ptr的存在不会增加指针对象的引用计数。要使用weak_ptr,必须先将其转换为shared_ptr。如果当前引用计数为正,那么将成功,并且将weak_ptr转换为shared_ptr将增加引用计数以表示转换后的指针是对象的“实际”引用。另一方面,如果引用计数已经为零(意味着指针对象已被销毁),则将weak_ptr转换为shared_ptr的尝试将失败。

A shared_ptr means shared ownership of the pointee object. The pointee object will remain in existence as long as at least one shared_ptr to that object exists, but as soon as the last shared_ptr to the object is destroyed, so will the pointee object.

shared_ptr表示指向对象的共享所有权。只要存在至少一个该对象的shared_ptr,指针对象将保持存在,但是一旦对象的最后一个shared_ptr被销毁,指针对象也将被销毁。

A weak_ptr means non-owning access to the pointee object. It allows access if the object exists. If the object has been destroyed, it tells you that the pointee object no longer exists rather than attempting to access the destroyed object.

weak_ptr表示对pointee对象的非拥有访问。如果对象存在,它允许访问。如果对象已被销毁,它会告诉您指针对象不再存在,而不是尝试访问被销毁的对象。

#1


19  

The fundamental conceptual difference between a naked pointer and a weak_ptr is that if the object pointed to is destroyed, the naked pointer won't tell you about it. This is called a dangling pointer: a pointer to an object that doesn't exist. They're generally hard to track down.

裸指针和weak_ptr之间的基本概念差异在于,如果指向的对象被破坏,裸指针将不会告诉您它。这称为悬空指针:指向不存在的对象的指针。他们通常很难追查。

The weak_ptr will. In order to use a weak_ptr, you must first convert it into a shared_ptr. And if that shared_ptr doesn't point to anything, then the object was deleted.

weak_ptr会。要使用weak_ptr,必须先将其转换为shared_ptr。如果shared_ptr没有指向任何内容,那么该对象就被删除了。

For example:

#include <iostream>
#include <memory>

std::weak_ptr<int> wp;

void test()
{
    auto spt = wp.lock(); // Has to be copied into a shared_ptr before usage
    if (spt) {
        std::cout << *spt << "\n";
    } else {
        std::cout << "wp is expired\n";
    }
}

int main()
{
    {
        auto sp = std::make_shared<int>(42);
        wp = sp;
        test();
    }
    test();
}

Output

42
wp is expired

#2


18  

A raw pointer is (at least normally) simply an address. You can't tell anything about what it points at from the pointer itself.

原始指针(至少通常)只是一个地址。你无法从指针本身告诉它指出什么。

A weak_ptr is always associated with a shared_ptr, so we probably need to start with a shared_ptr to make any sense of a weak_ptr.

weak_ptr总是与shared_ptr相关联,因此我们可能需要从shared_ptr开始才能理解weak_ptr。

A shared_ptr is reference counted, so it keeps track of how many references (pointers) to an object exist, and automatically destroys the object when no more references to that object exist.

shared_ptr是引用计数,因此它跟踪对象存在多少引用(指针),并在不再存在对该对象的引用时自动销毁该对象。

As I already said, a weak_ptr is associated with a shared_ptr. Unlike a shared_ptr, the existence of a weak_ptr does not increment the reference count for the pointee object. To use a weak_ptr, you must first convert it to a shared_ptr. If the current reference count is positive, that will succeed, and converting the weak_ptr to a shared_ptr will increment the reference count to signify that the converted pointer is a "real" reference to the object. If, on the other hand, the reference count is already zero (meaning the pointee object has already been destroyed) the attempt to convert the weak_ptr to a shared_ptr will simply fail.

正如我已经说过的,weak_ptr与shared_ptr相关联。与shared_ptr不同,weak_ptr的存在不会增加指针对象的引用计数。要使用weak_ptr,必须先将其转换为shared_ptr。如果当前引用计数为正,那么将成功,并且将weak_ptr转换为shared_ptr将增加引用计数以表示转换后的指针是对象的“实际”引用。另一方面,如果引用计数已经为零(意味着指针对象已被销毁),则将weak_ptr转换为shared_ptr的尝试将失败。

A shared_ptr means shared ownership of the pointee object. The pointee object will remain in existence as long as at least one shared_ptr to that object exists, but as soon as the last shared_ptr to the object is destroyed, so will the pointee object.

shared_ptr表示指向对象的共享所有权。只要存在至少一个该对象的shared_ptr,指针对象将保持存在,但是一旦对象的最后一个shared_ptr被销毁,指针对象也将被销毁。

A weak_ptr means non-owning access to the pointee object. It allows access if the object exists. If the object has been destroyed, it tells you that the pointee object no longer exists rather than attempting to access the destroyed object.

weak_ptr表示对pointee对象的非拥有访问。如果对象存在,它允许访问。如果对象已被销毁,它会告诉您指针对象不再存在,而不是尝试访问被销毁的对象。