相当于智能指针中的指针

问题描述 投票:0回答:2

我是指针新手,所以请考虑以下代码

#include <iostream>
#include <memory>
int main()
{
    double xNormal{5};
    double * pNormal = &xNormal;
    xNormal = 10;
    std::cout<<"value of variable we are pointing to is : "<<*pNormal<<std::endl;
    
    double xSmart{5};
    std::unique_ptr<double> pSmart = std::make_unique<double>(xSmart);
    xSmart = 10;
    std::cout<<"value of variable we are pointing to through a smart pointer is : "<<*pSmart<<std::endl;
}

这段代码的输出是

value of variable we are pointing to is : 10
value of variable we are pointing to through a smart pointer is : 5

在这段代码中,如果我们使用普通指针并且更改原始变量的值,则取消引用的指针也会“跟踪”该值,因为它指向该内存位置 当谈到智能指针时,我注意到没有这样的“跟踪”,也许我在理解上犯了错误,但问题是智能指针中的

double * pNormal = &xNormal;
相当于什么?

c++ pointers smart-pointers
2个回答
2
投票
std::unique_ptr<double> pSmart = std::make_unique<double>(xSmart);

此行创建一个 new 指针,其中包含 xSmart 在调用时的值。 它不会创建指向现有变量的指针。

不想使用

std::unique_ptr::reset

std::unique_ptr<bar> pointer;
pointer.reset(&xSmart);

如何将现有对象的地址分配给智能指针?

因为它会在销毁时删除本地的某些内容,正如所引用的答案所说。您不希望在自动(堆叠)对象周围使用

std::unique_ptr
std::shared_ptr


0
投票

std::make_unique<double>(xSmart)
的作用:

  1. 分配堆内存的
    sizeof(double)
    并将给定的 value 分配给它,就像调用
    new double{xSmart}
    一样。
  2. 返回指向新内存的
    std::unique_ptr<double>
    。智能指针在其析构函数中,一旦超出范围,就会
    delete
    分配的内存。
  3. 因此,
    std::unique_ptr
    应该永远不指向堆内存,如
    std::unique_ptr<double>{&xSmart}
    中所示,因为稍后尝试
    delete
    会导致崩溃和/或任意未定义的行为。

综上所述,

xSmart
指针中没有对
pSmart
的引用。
xSmart
中的值已复制到新的动态分配的
double
。下面是一些例子,有好有坏。

  1. 只是风格建议/意见:更喜欢引用而不是始终指向现有内容的指针,即不应包含
    nullptr
  2. 不要在家尝试这个。文档中,您可以发现自定义的
    Deleter
    类可以提供给
    std::unique_ptr
    。这就是我们在这里所做的。删除器“什么也不做”。这样,您就可以构造一个“智能”(实际上是简化)指针,该指针指向堆位置并且不会造成严重破坏。不用说,这充其量是完全无用且令人困惑的。 这大致基于您的动态智能指针情况。首先我们动态分配一个
  3. double
  4. 并将
    5
    分配给它。然后我们引用
    double
    。然后我们通过引用分配它并检查指向的位置(在堆上)是否真的发生了变化。是的,确实如此。这次我们使用
    std::default_delete<double>
    ,即模板的默认
    Deleter
    ,其中
    delete
    是当
    main()
    结束并且
    pUselessSmart
    的析构函数运行时分配的堆内存。
    
    
  5. #include <iostream> #include <memory> int main() { // Example 0: A local reference to a double on the stack. double xNormal{5}; double &rNormal{xNormal}; xNormal = 10; std::cout << "value of variable we are pointing to is : " << rNormal << std::endl; // Example 1: A “smart” pointer pointing at a double on the stack. double xSmart{5}; std::unique_ptr<double, void (*)(double *)> pSmart{&xSmart, +[](double *) {}}; xSmart = 10; std::cout << "value of variable we are pointing to through a smart pointer is : " << *pSmart << std::endl; // Example 2: A smart pointer pointing at a heap-allocated double. std::unique_ptr<double> pUselessSmart{std::make_unique<double>(5)}; double &rUselessSmart{*pUselessSmart}; rUselessSmart = 10; std::cout << "value of variable we are pointing to through a smart pointer is : " << *pUselessSmart << std::endl; }
© www.soinside.com 2019 - 2024. All rights reserved.