c++中 堆的一个奇怪现象
2011-09-18 11:04
316 查看
先看一下代码
但是如果加入了下列代码,竟然调用成功了,这是为什么呢
这时这段代码的输出为
为什么我对它撤销了,它还是可以输出它的值??
继续调试,修改代码如下:
这段代码还是有输出,它的输出如下
这时的输出变成0了,而且只有一个0,真奇怪。
请各位解答这种怪现象的原因。
SpreadsheetCell* firstCell = new SpreadsheetCell(22); //初始化firstCell堆,此时firstCell ->getString()和 getValue() 的值已经为22 cout << "cell 3 : " << firstCell->getValue() <<'\t' << firstCell->getString() << endl; delete firstCell; //此时已经撤销了firstCell cout << "cell 3 : " << firstCell->getValue() <<'\t' << firstCell->getString() << endl;//此时如果再用firstCell,会出现下列错误
但是如果加入了下列代码,竟然调用成功了,这是为什么呢
SpreadsheetCell* firstCell = new SpreadsheetCell(22); cout << "cell 3 : " << firstCell->getValue() <<'\t' << firstCell->getString() << endl; delete firstCell; SpreadsheetCell* testCell = new SpreadsheetCell(22); //创建一个新堆 //delete testCell;//暂时不撤销它 cout << "cell 3 : " << firstCell->getValue() <<'\t' << firstCell->getString() << endl;
这时这段代码的输出为
为什么我对它撤销了,它还是可以输出它的值??
继续调试,修改代码如下:
SpreadsheetCell* firstCell = new SpreadsheetCell(22); cout << "cell 3 : " << firstCell->getValue() <<'\t' << firstCell->getString() << endl; delete firstCell; SpreadsheetCell* testCell = new SpreadsheetCell(22); delete testCell; //此时对它撤销 cout << "cell 3 : " << firstCell->getValue() <<'\t' << firstCell->getString() << endl;
这段代码还是有输出,它的输出如下
这时的输出变成0了,而且只有一个0,真奇怪。
请各位解答这种怪现象的原因。
相关文章推荐
- C++的一个奇怪现象
- 关于C++ 中的插入符 << : 一个奇怪的现象
- C++ 一个奇怪的现象
- 在C++中使用C中字符串数组的一个奇怪问题。
- TextView结尾...结束的一个奇怪现象
- 一个onblur事件的奇怪现象
- 关于日期格式的一个奇怪的现象
- C的字符串指针一个奇怪的现象
- 发现ASP的一个奇怪现象
- 发现JS运算一个奇怪的现象
- Win32 C++工程中使用离屏面碰到的一个奇怪问题
- 今天发现一个奇怪的现象?
- 一个奇怪的现象,谁人能解释?
- 一个奇怪的现象
- 一个看起来奇怪的C++程序 && c++操作符重载
- overflow-x和overflow-y其中一个设置为visible时的奇怪现象
- 解释一个奇怪的并发现象
- 'xxx' declared as a 'virtual' field ----一个奇怪的C++bug
- C/C++—— 一个特别奇怪的C++程序
- 调试时发现的一个奇怪的现象,static int = 0时无法自增