什么是悬空的指针?我知道这是个很常见的问题,但对我来说还是新鲜的!我不明白悬挂指针的概念,在谷歌上搜索,并编写测试方法来找到一个。我只是想知道这是一个悬空的指针吗?无论我找到什么例子,都会返回一些东西,这里我正在尝试类似的东西!谢谢!void foo(const std::string name){
// will it be Dangling pointer?!, with comments/Answer
// it could be if in new_foo, I store name into Global.
// Why?! And what is safe then?
new_foo(name.c_str());}void new_foo(const char* name){
// print name or do something with name... }
3 回答
DIEA
TA贡献1820条经验 获得超3个赞
Class *object = new Class();Class *object2 = object;delete object;object = nullptr;// now object2 points to something which is not valid anymore
Object *method() {
Object object;
return &object;}Object *object2 = method();// object2 points to an object which has been removed from stack after exiting the functionc_strconst char *name
const char *namenew_foo
四季花海
TA贡献1811条经验 获得超5个赞
name
delete}}unique_ptr<T>T*unique_ptr::~unique_ptr
- 3 回答
- 0 关注
- 805 浏览
添加回答
举报
0/150
提交
取消
