作者IDontBite (大便兔子)
看板C_and_CPP
標題[問題] 刪不掉 囧
時間Tue Aug 25 16:47:18 2009
TypeDefine:
typedef struct node* ptr;
struct node{ int BF;
int key;
ptr lchild,rchild; };
main裡面:
ptr root = new node;
root->key = 2;
delete(root);
if(root)
cout<<root->key;
結果:
2
問題:
=_=這是...怎麼了...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.37.64.67
1F:→ phterry:我比較想知道,你delete了後,還期待它會印出什麼? 08/25 16:56
2F:推 littleshan:你 delete 之後再取用內容,結果是未定義 08/25 16:56
3F:→ IDontBite:我期待他甚麼也不印出@@ 08/25 16:57
4F:→ littleshan:而未定義的意思就是它可能是2、可能是亂數、可能當機 08/25 16:57
5F:→ IDontBite:如果成功刪掉的話if(root)不會過濾掉cout那行嗎? 08/25 16:58
6F:推 littleshan:delete 的意思是告訴系統 root 所指向的空間可歸還 08/25 16:59
7F:→ littleshan:並不會去改 root 指向的位置 08/25 16:59
8F:→ IDontBite:在刪除完之後,root不會只向NULL嗎 08/25 17:05
9F:推 VictorTom:delete過的pointer要自己指回NULL, 另外, pointer一定會 08/25 17:06
10F:→ VictorTom:存著一個值代表記憶體位置, 只是合不合法你能不能使用的 08/25 17:07
11F:→ VictorTom:問題, 沒有可能說什麼不印出東西, 只有那個東西有沒有意 08/25 17:08
12F:→ VictorTom:意正不正確而已; 而這應該是寫code自己要注意的, 不是由 08/25 17:08
13F:→ VictorTom:new/delete或malloc/free來幫你注意的:) 08/25 17:08
14F:→ IDontBite:感謝:P大概知道了 08/25 17:17
15F:→ james732:其實我以前也曾有類似的疑惑...XDDDD 08/25 18:50
16F:推 goodmike:原po是程式女俠~~ 08/25 21:12