在shared_ptr :: reset之后,可以使用本机ptr访问Obejct

时间:2015-09-17 08:08:32

标签: c++ c++11 smart-pointers

我想知道为什么我的本机指针在释放内存后仍能访问我的对象?

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

class A 
{
public:
    int a_;
    double b_;

    A(int a, double b) : b_(b), a_(a)
    {}
};

int main() {

    auto s_ptr = make_shared<A>(3,3.3);
    A* n_ptr = s_ptr.get();

    cout << n_ptr->b_ << endl;

    s_ptr.reset();

    cout << n_ptr->b_ << endl;

    return 0;
}

输出结果为:

3.3
3.3

也许内存被释放但仍然存储相同的值。在这种情况下,如何验证对象是否仍然存在?

谢谢!

0 个答案:

没有答案