虽然unique_ptr保证在移动后存储nullptr,它仍然指向对象? [英] Although unique_ptr guaranteed to store nullptr after move, it still is pointing to the object?

查看:189
本文介绍了虽然unique_ptr保证在移动后存储nullptr,它仍然指向对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC 5.2(C ++ 11)测试了以下代码:

  #include< iostream> 
#include< memory>

struct Foo
{
Foo(){std :: cout< Foo :: Foo\\\
; }
〜Foo(){std :: cout<< Foo ::〜Foo \\\
; }
void bar(){std :: cout<< Foo :: bar\\\
; }
};

void f(const Foo&)
{
std :: cout< f(const Foo&)\\\
;
}

int main()
{
std :: unique_ptr< Foo> p1(new Foo); // p1 owns Foo
if(p1)p1-> bar();

{
// p1-> bar();
std :: unique_ptr< Foo> p2(std :: move(p1)); // now p2 owns Foo
f(* p2);
p1-> bar();
if(p1 == nullptr)
{
std :: cout<<NULL<< std :: endl;
}
p1 = std :: move(p2); //所有权返回到p1
std :: unique_ptr< Foo> p3;
p3-> bar();
std :: cout<< destroying p2 ... \\\
;
}

if(p1)p1-> bar()

//当p1超出范围时,Foo实例被销毁
}


$ b b

现在我的问题是,虽然p1在移动操作后保证为nullptr,但它仍然指向上一个对象?



编辑:
是的,我问为什么p1-> bar();仍然工作给定p1已经移动,而bar()不是静态函数。
正如每个人都指出的,未定义的行为可能导致任何事情发生。
我现在已经运行到足够的未定义的行为...我将欣赏,如果有人可以指向我在C ++ 11中的常见未定义行为的集合。
提前感谢。

解决方案

我假设你是指:

  std :: unique_ptr< Foo> p2(std :: move(p1)); // now p2 owns Foo 
p1-> bar();

并输出Foo :: bar?



那么,它应该做什么呢?您正在解引用无效的指针。 任何可能发生的事情



在这种情况下,不访问不存在的 Foo 的任何成员,你会幸运的在这里崩溃(因为,在较低级别,没有实际的 http://stackoverflow.com/a/5249294/560648\">您所做的是错误的。


I've tested following code with GCC 5.2 (C++11):

#include <iostream>
#include <memory>

struct Foo
{
    Foo()      { std::cout << "Foo::Foo\n";  }
    ~Foo()     { std::cout << "Foo::~Foo\n"; }
    void bar() { std::cout << "Foo::bar\n";  }
};

void f(const Foo &)
{
    std::cout << "f(const Foo&)\n";
}

int main()
{
    std::unique_ptr<Foo> p1(new Foo);  // p1 owns Foo
    if (p1) p1->bar();

    {
        //p1->bar();
        std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
        f(*p2);
        p1->bar();
        if(p1==nullptr)
        {
            std::cout<<"NULL"<<std::endl;
        }
        p1 = std::move(p2);  // ownership returns to p1
        std::unique_ptr<Foo> p3;
        p3->bar();
        std::cout << "destroying p2...\n";
    }

    if (p1) p1->bar();

    // Foo instance is destroyed when p1 goes out of scope
}

So now my question is, although p1 is guaranteed to be nullptr after move operation, it seemed still is pointing to the previous object?

Edit: Yes, I'm asking why p1->bar(); still work given p1 already moved while bar() is not static function. As everybody pointed out, undefined behavior can cause anything to happen. I've been running into enough undefined behavior nowadays... I would appreciate if somebody can point me to a collection of common undefined behaviors in C++11. Thanks in advance.

解决方案

I assume you are referring to this:

std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
p1->bar();

and that it outputs "Foo::bar"?

Well, what should it do instead? You are dereferencing an invalid pointer. Anything can happen.

In this case, since you're not accessing any members of the non-existent Foo, you'd be bloomin' lucky to get a crash here (because, at a lower-level, no actual "physical" dereferencing of 0x0 need occur).

But it doesn't change the fact that what you're doing is wrong.

这篇关于虽然unique_ptr保证在移动后存储nullptr,它仍然指向对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆