C++ 智能指针范围和资源删除

标签 c++ function scope c++14 smart-pointers

我有以下简单的代码示例:

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

class resource
{
public:
   void print() { cout << "Class Still Alive" << endl; };
};

resource* create_resource()
{
   std::unique_ptr<resource> r = std::make_unique<resource>();
   return r.get();
}

void execute_resource(resource* r)
{
   r->print();
}

int main()
{
   resource* r = create_resource();
   execute_resource(r);
   return 0;
}

create_resource 中创建的唯一指针 r 在函数结束时超出范围。至少这是我对范围的理解。

那么由唯一指针包裹的实际资源如何仍然可以访问并且确实会产生段错误,因为它拥有的唯一指针应该超出范围并删除它?

编译使用:g++ test.cpp -std=c++14

最佳答案

,您对函数作用域的理解是正确的。 std::unique_ptr<resource>将在 create_resource 之后销毁功能范围。


So how is the actual resource wrapped by the unique pointer still reachable and does create a segmentation fault since its owning unique pointer should have gone out of scope and deleted this?

由于上述原因,您在 main 中收到的内容

resource *r = create_resource();

是一个悬挂指针。访问它将导致 undefined bahviour因此任何事情都可能发生。在您的情况下,这是一个段错误。


为了解决这个问题,您可以返回 std::unique_ptr<resource>本身来自 create_resource功能

#include <iostream>
#include <utility>
#include <memory>

class resource
{
public:
   void print() const // can be const
   { 
         std::cout << "Class Still Alive\n";
   };
};

std::unique_ptr<resource> create_resource()
{
   return std::make_unique<resource>(); // return the std::unique_ptr<resource>
}

void execute_resource(resource* r)
{
   r->print();
}

int main()
{
   auto r = create_resource();
   execute_resource(r.get()); // pass the pointer to execute!
   return 0;
}

关于C++ 智能指针范围和资源删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63158352/

相关文章:

jquery - 在单个页面中使用多个 Bootstrap Typeahead 字段?

使用原型(prototype)的 Javascript 类变量范围

c++ - #pragma pack、模板类型定义和结构对齐

c++ - cout奇怪的输出

c++ - 如何提高疯子候机程序的效率

C返回多个值

c++ - 如何使用wxwidget连接到mysql?

javascript - 如何验证出生年份?

ios - 当你在第一个 VC 中时,如何在第二个 VC 中运行一个函数?

javascript - 函数没有返回值 : scope issue