为什么不仅仅使用delete []来删除2D动态分配的数组? [英] Why it is not just enough to use delete [] to delete the 2D dynamically allocated array?

查看:64
本文介绍了为什么不仅仅使用delete []来删除2D动态分配的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建了一个2D动态分配的数组:

I have used the following code to create a 2D dynamically allocated array:

int x,y;
    cout<<"Entre matrix dimensions"<<endl;
    cin>>x>>y;

    //Dynamically Allocating x*y array
    int **myArray = new int*[x];              //reserve x row elements
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < y; ++j) {
            myArray[i] = new int[j];           //makes each one of the reserved row elements points to an array
        }
    }

    //entreing the values
    cout<<"Entre the values"<<endl;
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < y; ++j) {
            cin>>myArray[i][j] ;
        }
    }

    //Displaying the values
    cout<<"You entred : "<<endl;
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < y; ++j) {
            cout<<myArray[i][j]<<" " ;
        }
        cout<<endl;
    }



当我测试前面的代码时,它运行良好。但是当我尝试使用以下代码解除分配2d数组时:


When i tested the previous code, it worked well.But when i tried the following code to deallocate the 2d array:

 //Memory Deallocation
 for (int i = 0; i < x; ++i) {
         delete [] myArray[i];
     }
delete[] myArray;



发生错误:


An error occured:

Heap corruption detected. CRT detected that the application wrote to memory after end of heap buffer





我尝试过:



我曾尝试使用



What I have tried:

I have tried to just use

delete[] myArray;

进行内存释放,但未显示错误消息。但我不知道是对还是错?

for memory deallocation and the error message did not appear. But i do not know is that right or wrong?

推荐答案

那是因为你的删除代码不匹配分配代码:

That is because your delete code does not match the allocation code:
//Dynamically Allocating x*y array
int **myArray = new int*[x];              //reserve x row elements
for (int i = 0; i < x; ++i) {
    for (int j = 0; j < y; ++j) {
        myArray[i] = new int[j];           //makes each one of the reserved row elements points to an array
    }
}
//Memory Deallocation
for (int i = 0; i < x; ++i) {
    delete [] myArray[i];
}
delete[] myArray;

对于每个 x ,您要分配 y 数组全部分配给 myArray [i] 。因此,只会使用最后一个分配的(并且稍后释放)。此最后一个的大小为 y-1 ,这是堆损坏的原因,因为您正在访问另一个(未分配)项目。



只需使用一个循环进行分配就像删除时一样:

For each x you are allocating y arrays which are all assigned to myArray[i]. So only the last allocated one will be used (and later freed). Also this last one has the size of y-1 which is the reason for the heap corruption because you are accessing one more (not allocated) item.

Just use a single loop for allocation like when deleting:

for (int i = 0; i < x; ++i) {
    myArray[i] = new int[y];
}


你的删除没问题。你的内存分配是错误的。

尝试:

Your delete is fine. Your memory allocation is wrong.
Try:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int x,y;
  cout<<"Entre matrix dimensions"<<endl;
  cin>>x>>y;
  cout << "x = " << x << ", y = " << y << endl;


  //Dynamically Allocating x*y array
  int **myArray = new int * [x];              //reserve x row elements
  for (int i = 0; i < x; ++i)
    myArray[i] = new int[y];

  cout << "assigning random values"<< endl;
  for (int i = 0; i < x; ++i)
    for (int j = 0; j < y; ++j)
      myArray[i][j] = rand();

  //Displaying the values
  cout<<"the values : "<<endl;
  for (int i = 0; i < x; ++i)
  {
    for (int j = 0; j < y; ++j)
      cout<<myArray[i][j]<<" " ;
    cout << endl;
  }

  //Memory Deallocation
  for (int i = 0; i < x; ++i)
    delete [] myArray[i];

  delete[] myArray;
}





那么标准库会轻轻地为你提供向量容器,你为什么不使用它?



By the way, the standard library gently provides you the vector container, why don't you use it?


你已经为你的数组分配了正确的内存,所以你得到了这个烂摊子。您在删除(正确分配但误用的)内存块时错误 WHEN 访问无效内存和 NOT



此代码应该完成这项工作:
You have allocated the correct memory for your array, so you get this mess. You the error WHEN accessing invalid memory and NOT when deleting (the correct allocated but misused) memory block.

this code should do the job:
int *myArray = new int[x * y];//allocate array of size (fixed the wrong "+" operator to "*"
delete[] myArray;//cleanup 



提示:使用内存视图窗口查看数据并稍微玩一下。; - )


Tip: use memory view window to see the data and play a bit with it. ;-)


这篇关于为什么不仅仅使用delete []来删除2D动态分配的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆