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

 

先看一下代码

 

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

请各位解答这种怪现象的原因。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值