What’s the proper use of the unique_ptr?

I’m currently struggling understanding the purpose of unique_ptr.

I would like to have some kind of reference somewhere else of the content of std::unique_ptr<Component> component stored inside any instance of the Object class. Since we are using smart pointer, the raw pointer pComp should NEVER manage memory itself because the unique_ptr is the sole responsible for it, and this one of the core principle of Smart Pointers. However, what confuses me is that once we go out of scope and the destructor of unique_ptr is called, which in turn free the memory, there’s no way for me to check if the memory is still valid on some other part of my code that are referencing, but not owning, to the same memory address (in this case, pComp).
I understand that using a unique_ptr is a very clear statement about the fact that Object is the owner of that memory and no one else should be responsible for it, but then what’s the point of using it if the rest of my code can’t check if the memory is still valid?
Why unique_ptr do not set their internal pointer to nullptr? Am I misunderstanding the use of unique_ptr? Is there a correct way to do this? If this is a case where I should use shared_ptr, why so since I want Object to be the only responsible? Thanks to everyone in advance.

#include <iostream>
#include <memory>

class Component
{
public:
    Component() { std::cout << "Component Constructor" << std::endl; }
    ~Component() { std::cout << "Component Destructor" << std::endl; }
};

class Object
{
public:
    std::unique_ptr<Component> component;

    Object() : component(std::make_unique<Component>()) { std::cout << "Object Constructor" << std::endl; }
    ~Object() { std::cout << "Object Destructor" << std::endl; }
};

int main()
{
    Component* pComp = nullptr;
    {
        Object obj;
        pComp = obj.component.get();
    }

    if (pComp == nullptr)
        std::cout << "pComp is nullptrn"; // This code is never reached.

    return 0;
}

New contributor

Federico Dell’Aquila is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

In your example, pComp has a copy of the pointer value of that stored by obj.component. When obj is destructed, copies of that pointer value become invalid. There is no way in C++ to test for invalid pointers, it is up to the programmer to ensure they are never accessed.

Why unique_ptr do not set their internal pointer to nullptr?

The unique_ptr doesn’t null its underlying pointer in its destructor, because that pointer is about to cease to exist.

Is there a correct way to do this?

No, you should either extend the lifetime of obj, or take ownership of obj.component with your own unique_ptr. Only if you can’t do either of those should you consider changing to shared_ptr.

So use unique_ptr instead of pointers. You will then get:

int main()
{
    std::unique_ptr<Component> pComp;
    {
        Object obj;
        pComp = obj.component; // compile error
    }
}

It’s up to you if you want to std::move component out of object or you have to use the objects component.

then what’s the point of using it if the rest of my code can’t check if the memory is still valid?

RAII style automatic memory deallocation.

Why unique_ptr do not set their internal pointer to nullptr?

Why would it? It’s an instruction that has to execute, that is time.

Is there a correct way to do this?

It’s up to you what you define as “correct” semantics with your object. C++ is just hammer, it’s up to where you hit.

where I should use shared_ptr, why so since I want Object to be the only responsible?

Sounds like no, bit I think you also want to make unique_ptr const so you can’t move out of it.

class Object
{
public:
    const std::unique_ptr<Component> component;

...
    {
        Object obj;
        pComp = std::move(obj.component); // also compile error
    }

Than the way the only way will be to use obj.component to get to the component. But then, you still can use .get() and get raw pointer. It’s up to the programmer to write proper code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật