1

最近これについて質問を投稿しましたが、これは別の質問です。delete [] matrix動的メモリ割り当てを使用して 2D 配列を作成しました。行列が使用された後、削除してメモリを解放する必要があります。以下のコードのメソッドの代わりに削除するだけで使用できない理由がわかりません。

int **matrix;

    // dynamically allocate an array
    matrix = new int *[row]; 
    for (int count = 0; count < row; count++)
        matrix[count] = new int[col];

    // free dynamically allocated memory
    for( int i = 0 ; i < *row ; i++ )
    {
        delete [] matrix[i] ;
        delete [] matrix ;
    }

問題はmain()、2D配列を作成し、他のint **関数を使用して値を割り当てるため、割り当てられたメモリを削除する方法がわからないため、ループによりランタイムエラーが発生するためです

    int main()
    {
        int **matrixA = 0, **matrixB = 0, **matrixResult = 0; // dynamically allocate an array
        int rowA, colA, rowB, colB; // to hold the sizes of the matrices

        // get values for input method
        int inputMethod = userChoiceOfInput();
        if (inputMethod == 1) // select input by keyboard
        {
            cout << "Matrix A inputting...\n";
            matrixA = getMatricesByKeyboard(&rowA, &colA);
            cout << "Matrix B inputting...\n";
            matrixB = getMatricesByKeyboard(&rowB, &colB);
        }
        else if (inputMethod == 2) // select input by files
        {
            matrixA = getMatricesByFileInput("F:\\matrixA.txt", &rowA, &colA); 
            matrixB = getMatricesByFileInput("F:\\matrixB.txt", &rowB, &colB); 
        }

        //addition(matrixA, &rowA, &colA, matrixB, &rowB, &colB);

        cout << matrixA[1][0];  

////////////////////////run time error///////////////////////
    // free allocated memory of matrix A
    for( int i = 0 ; i < rowA ; i++ )
    {
        delete [] matrixA[i] ;
        delete [] matrixA ;
    }
    // free allocated memory of matrix B
    for( int i = 0 ; i < rowB ; i++ )
    {
        delete [] matrixB[i] ;
        delete [] matrixB ;
    }
////////////////////////run time error///////////////////////

        // free allocated memory of matrix A
        delete [] matrixA ; // i dont know what would these delete
        delete [] matrixB ; 

        return 0;
    }
4

3 に答える 3

13

マトリックスを調べて、各配列を削除する必要があります。それが終わったら、マトリックス自体を削除できます

// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
    delete[] matrix[i]; // delete array within matrix
}
// delete actual matrix
delete[] matrix;
于 2013-02-12T09:49:55.500 に答える
4

とにかく動的配列を使用している場合は、std::vector を使用することを強くお勧めします。パフォーマンスの低下はほとんどなく、標準アルゴリズムをベクトルでより適切に使用できることを考えると、コードのパフォーマンスが大幅に向上する可能性があります。

unsigned int cols=40, rows=35;
std::vector<int> temp(cols,0); //this is only created so that we can initialize a 
                               //row at a time, the first parameter is the amount of 
                               //elements to initialize with and the second is the value
                               //to initialize with
std::vector<std::vector<int>> matrix(rows,temp); //creates a vector with 35 vectors each
                                                 //initialized with the values in temp

matrix[2][3] = 4;              //use just like a normal array
matrix.resize(88,temp);         //you can add rows later too
matrix.push_back(temp);         //or like this

//probably the most important, you don't need to delete because you never needed to 
//use new in the first place

new と delete を使用することは、正当な理由により、実際には最新のスタイルではありません。C++ and Beyond 規約の達人によると、パフォーマンスの最適化の場合とライブラリ コードを記述する場合にのみ使用する必要があります。多くの本や教師は今でもこのように教えていますが、一方でほとんどの本はがらくたです。その中の宝石は次のとおりです。The Definitive C++ Book Guide and List

于 2013-02-12T09:53:39.793 に答える
0

int* 型にはデストラクタがないため、少なくとも必要なデストラクタはありません。このようなことを行うことで、すべてのメモリの割り当て/割り当て解除を回避できます

std::vector<int*> matrix(rows);
std::vector<int> allData(rows * cols);
for(int i = 0; i < rows; i++)
{
matrix[i] = &allData[i * cols];
}

または、boost::numeric::ublas::matrix のような標準コンテナーを使用します。

于 2013-02-12T10:05:48.310 に答える