3

所以,我有这段代码,我试图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
}
4

5 回答 5

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;

但是,在 C++ 中,我们更喜欢不要乱用动态分配的数组。使用 astd::vector<std::vector<int>>或 a std::array<std::array<int, 4>, 4>。如果您关心数据的局部性,请尝试boost::multi_array.

于 2013-02-18T20:53:34.110 回答
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 编码。

于 2013-02-18T21:16:10.670 回答
1

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

但是,比起手动数组,更喜欢标准容器。

std::vector< std::vector<int> > iv; // dynamic size
std::array< std::array<int,4>, 4> ia; // static size
于 2013-02-18T20:53:47.540 回答
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;
        }
    }
}
于 2013-02-18T20:53:47.743 回答
1

您需要delete为每个较早的电话打一次电话new

for(int i = 0; i < 4; i++ ) {
    delete[] ppint[i];
}
delete[] ppint;
于 2013-02-18T20:54:51.513 回答