【发布时间】:2014-12-17 20:55:10
【问题描述】:
这会导致我的程序出现断言失败:
int a = 5;
std::unique_ptr<int>intptr(&a);
错误
文件:f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp
行:32
表达式:_BLOCK_TYPE_IS_VALID(pHead > nBlockUse)
当我使用“new”或“= make_unique (..)”初始化它时效果很好,但我很想知道为什么我不能通过给出现有变量的地址来初始化它。
【问题讨论】:
-
您不能从堆栈分配或释放内存。由于
a已在堆栈中声明,因此您无法删除它,这是您的unique_ptr在超出范围时尝试执行的操作。 -
出于兴趣,您想在这里达到什么目的?
-
doctorlove -> 没什么,我只是为了学习而尝试一些东西
-
出于学习目的,在 C++ 中使用堆越少越好。
标签: c++ smart-pointers unique-ptr