您的位置:首页 > 编程语言 > C语言/C++

c++中 堆的一个奇怪现象

2011-09-18 11:04 316 查看
先看一下代码

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,真奇怪。

请各位解答这种怪现象的原因。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: