去除涂层动态分配的多维数组。

时间:2013-02-18 20:52:15

标签: c++ arrays heap dynamic-arrays memory-management

所以,我有这个代码,我试图在最后解除分配数组ppint。我曾尝试使用Xcode泄漏来确定它是否正常工作,但我不太明白。会这样做吗?

delete ppint[0];
delete ppint[1];
delete ppint[2];
delete ppint[3];

或者还有其他必须要做的事吗?

#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;

int main()
{
    int **ppint;
    ppint = new int * [4];

    for(int i = 0; i < 4; i++ ) {
        ppint [i] = new int[4];
    } // declares second layer of arrays

    for(int i = 0, count = 0; i < 4; i++ ) {
        for(int j = 0; j < 4; j++ ) {
            count++;
            ppint [i] [j] = count;
        } //init part 2
    } // init array

    for(int i = 0; i < 4; i++ ) {
        for(int j = 0; j < 4; j++ ) {
            cout << ppint [i] [j] << endl;
        } // print part 2
    } //print array
}

5 个答案:

答案 0 :(得分:6)

关闭。您必须使用delete[],因为您使用new[]

分配了每个delete[] ppint[0]; delete[] ppint[1]; delete[] ppint[2]; delete[] ppint[3];
for(int i = 0; i < 4; i++ ) {
  delete[] ppint[i];
}

但当然,你应该使用一个循环:

delete[]

然后不要忘记ppint delete[] ppint; 本身:

std::vector<std::vector<int>>

但是,在C ++中,我们宁愿不搞乱动态分配的数组。使用std::array<std::array<int, 4>, 4>或{{1}}。如果您关心数据的位置,请尝试boost::multi_array

答案 1 :(得分:2)

我的解决方案是:

#include<iostream>
#include<cstdlib>

using namespace std;

int main(){

    int ** twod;
    twod = new int*[4];
    int counter = 0;
    /*init 2d variable and check whether we got the memory*/
    if ( twod == NULL) {
        exit(EXIT_FAILURE);
    }
    for (unsigned i = 0; i< 4; i++){
        /**/
        twod[i] = new int[4];
        if (twod[i] == NULL){
            exit(EXIT_FAILURE);
        }
        for (unsigned j = 0; j < 4; j++){
            counter++;
            twod[i][j]=counter;
        }
    }

    for ( unsigned i = 0; i < 4; i++){
        for (unsigned j = 0; j < 4; j++){
            cout << twod[i][j] << endl ;
        }
    }

    for (unsigned i = 0; i < 4; i++)
        delete [] twod[i];

    /*and don't forget to delete the int* array as well.*/
    delete [] twod;

}

如果你想确保你没有犯任何内存错误,也可以使用valgrind:

Valgrind是检测内存错误的绝佳工具。在输出中,它显示我们已经释放了5个内存分配。 Valgrind还可以显示其他类型的内存错误,例如您根本没有分配的内存使用情况。使用在使用前未正确初始化的内存,就像我说的一个优秀的内存检查工具。

$ valgrind ./a.out
==18376== Memcheck, a memory error detector
==18376== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==18376== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==18376== Command: ./a.out
==18376== 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
==18376== 
==18376== HEAP SUMMARY:
==18376==     in use at exit: 0 bytes in 0 blocks
==18376==   total heap usage: 5 allocs, 5 frees, 96 bytes allocated
==18376== 
==18376== All heap blocks were freed -- no leaks are possible
==18376== 
==18376== For counts of detected and suppressed errors, rerun with: -v
==18376== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

当然其他人告诉你使用std :: vector,例如你用c ++编写而不是c。

答案 2 :(得分:1)

您仍然需要删除ppint本身指向的内存。您还需要使用delete[]而不是delete

但是,更喜欢标准容器而不是手动数组。

std::vector< std::vector<int> > iv; // dynamic size
std::array< std::array<int,4>, 4> ia; // static size

答案 3 :(得分:1)

分配有new[]的内容需要使用delete[]取消分配。但是使用std::vector而不是原始数组和new。它会自动为您管理内存。


以下模拟您的原始代码,并且更短,更清洁,更安全:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> v( 4, vector<int>( 4 ) );

    for( int i = 0, count = 0; i < 4; ++i ) {
        for( int j = 0; j < 4; ++j ) {
            ++count;
            v[i][j] = count;
        }
    }

    for( int i = 0; i < 4; ++i ) {
        for( int j = 0; j < 4; ++j ) {
            cout << v[i][j] << endl;
        }
    }
}

更多本着C ++的精神,你可以/应该定义一个可重用的矩阵类,例如

#include <iostream>
#include <vector>
using namespace std;

template< class item_t >
class matrix_
{
private:
    vector<item_t>  items_;
    int             width_;
    int             height_;

    int index_for( int const x, int const y ) const
    {
        return y*width_ + x;
    }

public:
    item_t const& operator()( int const x, int const y ) const
    {
        return items_[index_for( x, y )];
    }

    item_t& operator()( int const x, int const y )
    {
        return items_[index_for( x, y )];
    }

    matrix_( ssize_t const w, ssize_t const h )
        : items_( w*h )
        , width_( w )
        , height_( h )
    {}
};

int main()
{
    matrix_<int> m( 4, 4 );

    for( int i = 0, count = 0; i < 4; ++i ) {
        for( int j = 0; j < 4; ++j ) {
            ++count;
            m( j, i ) = count;
        }
    }

    for( int i = 0; i < 4; ++i ) {
        for( int j = 0; j < 4; ++j ) {
            cout << m( j, i ) << endl;
        }
    }
}

答案 4 :(得分:1)

对于delete

之前的每次调用,您需要拨打new
for(int i = 0; i < 4; i++ ) {
    delete[] ppint[i];
}
delete[] ppint;