delete p;发生了什么?

测试代码

#include <iostream>
using namespace std;

void test()
{
    int *p;
    p = new int;
    *p = 5;
    cout <<" *p = " <<*p <<endl;
    cout <<"  p = " <<p <<endl;
    cout <<" &p = " <<&p <<endl;

    delete p;
    cout << "after delete" <<endl;
    cout << " *p = " <<*p <<endl;
    cout << "  p = " <<p <<endl;
    cout << " &p = " <<&p <<endl;
}
int main(int argc, char *argv[])
{
	test();
	return 0;
}

测试结果

在这里插入图片描述

结论

从运行结果上看,delete p;这个操作并没有改变p的指向,也没有改变p指向的内容,应该是向操作系统表明一个消息: p指向的这片内存操作系统可以回收了。

猜你喜欢

转载自blog.csdn.net/weixin_43479963/article/details/104758965