unique_ptr堆和堆栈分配 [英] unique_ptr heap and stack allocation

查看:121
本文介绍了unique_ptr堆和堆栈分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始指针可以指向分配在堆栈或堆上的对象.

Raw pointers can point to objects allocated on the stack or on the heap.

堆分配示例:

// heap allocation
int* rawPtr = new int(100);
std::cout << *rawPtr << std::endl;      // 100

堆栈分配示例:

int i = 100;
int* rawPtr = &i;
std::cout << *rawPtr << std::endl;      // 100

使用auto_ptr示例进行堆分配:

Heap allocation using auto_ptr example:

int* rawPtr = new int(100);
std::unique_ptr<int> uPtr(rawPtr);
std::cout << *uPtr << std::endl;        // 100

使用auto_ptr示例进行堆栈分配:

Stack allocation using auto_ptr example:

int i = 100;
int* rawPtr = &i;
std::unique_ptr<int> uPtr(rawPtr);      // runtime error

智能指针"是否打算用于指向堆上动态创建的对象?对于C ++ 11,我们是否应该继续使用原始指针指向堆栈分配的对象?谢谢.

Are 'smart pointers' intended to be used to point to dynamically created objects on the heap? For C++11, are we supposed to continue using raw pointers for pointing to stack allocated objects? Thank you.

推荐答案

智能指针通常用于指向使用new分配并使用delete删除的对象.不必以这种方式使用它们,但是如果我们想猜测语言构造的预期用途,那似乎就是意图.

Smart pointers are usually used to point to objects allocated with new and deleted with delete. They don't have to be used this way, but that would seem to be the intent, if we want to guess the intended use of the language constructs.

在上一个示例中代码崩溃的原因是由于用delete删除"部分.当它超出范围时,unique_ptr将尝试delete它具有指向的对象.由于它是在堆栈上分配的,因此失败.就像您写过一样delete rawPtr;

The reason your code crashes in the last example is because of the "deleted with delete" part. When it goes out of scope, the unique_ptr will try to delete the object it has a pointer to. Since it was allocated on the stack, this fails. Just as if you had written, delete rawPtr;

由于通常将智能指针与堆对象一起使用,因此有一种功能可以一次性在堆上分配并转换为智能指针. std::unique_ptr<int> uPtr = make_unique<int>(100);将执行第三个示例的前两行的操作.共享指针也有一个匹配的make_shared.

Since one usually uses smart pointers with heap objects, there is a function to allocate on the heap and convert to a smart pointer all in one go. std::unique_ptr<int> uPtr = make_unique<int>(100); will perform the actions of the first two lines of your third example. There is also a matching make_shared for shared pointers.

可以将智能指针与堆栈对象一起使用.您要做的是指定智能指针使用的删除器,提供不调用delete的删除器.由于它是一个堆栈变量,不需要执行任何操作即可删除它,因此删除器无法执行任何操作.有人问,智能指针的作用是什么,如果它所做的只是调用一个什么都不做的函数呢?这就是为什么您通常不会看到与堆栈对象一起使用的智能指针的原因.但这是一个显示一些有用性的示例.

It is possible to use smart pointers with stack objects. What you do is specify the deleter used by the smart pointer, providing one that does not call delete. Since it's a stack variable and nothing need be done to delete it, the deleter could do nothing. Which makes one ask, what's the point of the smart pointer then, if all it does is call a function that does nothing? Which is why you don't commonly see smart pointers used with stack objects. But here's an example that shows some usefulness.

{
    char buf[32];
    auto erase_buf = [](char *p) { memset(p, 0, sizeof(buf)); };
    std::unique_ptr<char, decltype(erase_buf)> passwd(buf, erase_buf);

    get_password(passwd.get());
    check_password(passwd.get());
}
// The deleter will get called since passwd has gone out of scope.
// This will erase the memory in buf so that the password doesn't live
// on the stack any longer than it needs to.  This also works for
// exceptions!  Placing memset() at the end wouldn't catch that.

这篇关于unique_ptr堆和堆栈分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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