"删除"运算符C ++

时间:2017-03-23 02:45:31

标签: c++ pointers memory-management

我理解的是,如果您使用"删除"指向堆上的内存地址的指针上的运算符,它释放内存。我编写了这段代码来测试我的知识,但即使使用了delete操作符,内存地址仍然具有地址的值。有谁能解释一下这里发生了什么?

#include <iostream>

using namespace std;

int main()
{
   int* aPtr = new int(5);
   cout << "address of int in heap is " << aPtr << endl;
   cout << "value of int in heap is " << *aPtr << endl;
   delete aPtr;
   cout << "address of int in heap is " << aPtr << endl;
   cout << "value of int in heap is " << *aPtr << endl;
   return 0;
}

我得到的控制台输出在这里,

address of int in heap is 0x7fe851402700
value of int in heap is 5
address of int in heap is 0x7fe851402700
value of int in heap is 5

0 个答案:

没有答案