小编Mir*_*zov的帖子

在动态对象数组上调用delete []不会释放内存?

在以下代码中:

int main(int argc,char * argv[]){
  int * ptr;
  ptr = 0; // tried also with NULL , nothing changes
  ptr = new int[10]; // allocating 10 integers
  ptr[2] = 5;
  ptr[15] = 15;  // this should cause error (seg fault) - but it doesn't

  cout << ptr[2] << endl;
  cout << ptr[15] << endl;  // no error here

  delete [] ptr;

  cout << ptr[2] << endl;   // prints the value 5
  cout << ptr[15] << endl;  // prints the …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management dynamic-allocation

2
推荐指数
1
解决办法
641
查看次数