作者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