'delete'关键字如何在以下c ++程序中起作用?

时间:2017-03-16 14:33:46

标签: c++ dynamic-memory-allocation

任何人都可以指导我完成代码吗?我不确定删除应该如何工作。即使在重新分配后,我仍然能够修改堆内存的内容吗?

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
  cout << "------ Introducing Heap mem allocation ------" << endl;

  int *p = new int; // allocation
  *p = 1114; //set the content

  cout << p << "   " << *p << endl; //check the address p holds and the content

  delete p; // What happens here? Shouldn't the address held by p get deleted?
  cout << p << "   " << *p << endl; // this produces no change except that the content is set to 0

  *p = 20; // but I can still modify the content
  cout << p << "   " << *p << endl; // What am I missing? How does 'delete' actually work? HELP

  return 0;
}

0 个答案:

没有答案