Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

free allocated memory in 2D dynamic memory allocation array in C++

I recently posted a question about this, but this is a different question. I created a 2D arrays using dynamic memory allocation, after the matrix is used, we need to free the memory by delete it and I dont understand why we cant just use delete [] matrix to delete it instead of the method in the codes below

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 ;
    }

Because the problem is because in main() i created a 2D array and assign the values using other int ** functions, i dont know how to delete the allocated memory, the loop will cause runtime error

    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...
";
            matrixA = getMatricesByKeyboard(&rowA, &colA);
            cout << "Matrix B inputting...
";
            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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to go through your matrix and delete each array. At the end of doing that you can delete the matrix itself

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...