什么是懸空的指針?我知道這是個很常見的問題,但對我來說還是新鮮的!我不明白懸掛指針的概念,在谷歌上搜索,并編寫測試方法來找到一個。我只是想知道這是一個懸空的指針嗎?無論我找到什么例子,都會返回一些東西,這里我正在嘗試類似的東西!謝謝!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 function
c_str
const char *name
const char *name
new_foo

四季花海
TA貢獻1811條經驗 獲得超5個贊
name
delete
}
}
unique_ptr<T>
T*
unique_ptr::~unique_ptr
- 3 回答
- 0 關注
- 665 瀏覽
添加回答
舉報
0/150
提交
取消