【发布时间】:2015-11-28 05:01:02
【问题描述】:
我编写了简单的代码来帮助我理解智能指针:
string s = "str";
vector <unique_ptr<string>> pv ;
pv.push_back(unique_ptr<string>(&s));
cout<<*(pv[0])<<endl;
这段代码编译得很好,但给我一个运行时错误:
str * `...' 中的错误:munmap_chunk():无效指针:0x00007ffd956e57e0 * 中止(核心转储)
发生了什么,我做错了什么?
【问题讨论】:
-
一个
unique_ptr到堆栈上的东西没有意义。
标签: c++11 vector runtime-error smart-pointers unique-ptr