先看一下代码
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,真奇怪。
请各位解答这种怪现象的原因。