简体   繁体   English

如何删除 CPP 中的动态非矩形二维数组

[英]How do I delete dynamic non-rectangular 2-dimensional Arrays in CPP

This is some code I wrote just to understand working with pointers.这是我编写的一些代码,只是为了理解如何使用指针。 Here I want to allocate and display a 2-dimensional Pointerarray这里我要分配并显示一个二维的Pointerarray

int main()
{
    constexpr int size = 10;
    int** _2darray = new int* [size];

    for ( int i = 0; i < size; i++ )
    {
        *(_2darray + i) = new int[i];
        for ( int j = 0; j <= i; j++ )
        {
            *(*(_2darray + i) + j) = j;
        }
    }

    for ( int i = 0; i < size; i++ )
    {
        for ( int j = 0; j <= i; j++ )
        {
            std::cout << _2darray[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

This will result in printing this:这将导致打印:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9

But if I want to prevent a memory leak now, I would want to do this:但是如果我现在想防止内存泄漏,我想这样做:

for ( int i = 0; i < size; i++ )
  {
       delete[] _2darray[i];
  }
  delete[] _2darray;

Unfortunately this will give me an exception that the heap was corrupted不幸的是,这会给我一个异常,即堆已损坏

在此处输入图片说明

I guess that it crashes because it doesen't know the exact size of the subarrays, but I also heard that delete always keeps track of how much bytes are being allocated for a array.我猜它崩溃是因为它不知道子数组的确切大小,但我也听说delete总是跟踪为数组分配了多少字节。

Thank you for any short answer.感谢您的简短回答。

Your delete logic is correct.您的删除逻辑是正确的。

The error message means that your program is writing to memory that is out of bounds.该错误消息意味着您的程序正在写入越界的内存。 The comparison j <= i is not what is incorrect here however -- I mean assuming the program is currently generating the output you want.但是,比较j <= i在这里并不是不正确的——我的意思是假设程序当前正在生成您想要的输出。

Just note that you want your i th array to contain i+1 items.请注意,您希望第i个数组包含i+1项目。 Specifically notice the zeroth array must contain one item (namely zero) so *(_2darray + i) = new int[i] should be *(_2darray + i) = new int[i+1] .特别注意第零个数组必须包含一项(即零),所以*(_2darray + i) = new int[i]应该是*(_2darray + i) = new int[i+1]

With that change in place the for loops with the j <= i comparisons are correct.有了这个变化,带有j <= i比较的 for 循环是正确的。 Index j is going to iterate over each item in the i th array which contains i+1 items.索引 j 将迭代包含i+1个项目的第i个数组中的每个项目。 The comparison j < i will yield only i iterations when starting from zero so you do need j <= i in this case even though such a comparison is non-standard looking.当从零开始时,比较j < i将只产生i次迭代,因此在这种情况下您确实需要j <= i即使这样的比较看起来是非标准的。 Consider j < i+1 , which is equivalent but might be clearer.考虑j < i+1 ,它是等价的,但可能更清楚。

for ( int i = 0; i < size; i++ )
{
    *(_2darray + i) = new int[i];
     //...
}  

For first iteration, when i=0 you do:对于第一次迭代,当i=0您执行以下操作:

*(_2darray + i) = new int[0];  

This is not a good way to allocate memory.这不是分配内存的好方法。 see this C++ new int[0] — will it allocate memory?看到这个C++ new int[0] — 它会分配内存吗?
And after you do:在你这样做之后:

for ( int j = 0; j <= i; j++ )

it should be:它应该是:

for ( int j = 0; j < i; j++ )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM