代码之家  ›  专栏  ›  技术社区  ›  PeterK

二维阵列分配问题

  •  2
  • PeterK  · 技术社区  · 15 年前

    这是我朋友昨天被问到的一个面试问题。问题是:这个程序是否会因“访问冲突”错误而崩溃?我看了一会儿,觉得不行,不行。但事实证明,在Visual Studio中进行这种尝试是错误的。我不知道这里发生了什么…或者更准确地说,我知道发生了什么,但不明白为什么。问题似乎是Matrix2数组根本没有被分配。

    代码如下:

    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int** matrixAlloc( const int rows, const int cols );
    void matrixAlloc( int** matrix, const int rows, const int cols );
    void matrixDealloc( int** m, const int rows);
    void matrixPrint( const int* const * const m, const int rows, const int cols );
    
    int main( int argc, char** argv )
    {   
        srand( (unsigned int)time( NULL ) );
        int** matrix1 = matrixAlloc( 4, 5 );
        matrixPrint( matrix1, 4, 5 );
        matrixDealloc( matrix1, 4 );
    
        int ** matrix2 = NULL;
        matrixAlloc( matrix2, 4, 5 );
        matrixDealloc( matrix2, 4 ); // <--- crash occurs here  
    }
    
    int** matrixAlloc( const int rows, const int cols )
    {
        int **matrix = new int *[ rows ];
        for ( int i = 0; i < rows; i++ )
        {
            matrix[ i ] = new int[ cols ];
            for ( int j = 0; j < cols; j++ )
            {
                matrix[ i ][ j ] = (rand() * 12347) % 10;
            }
        }
    
        return matrix;
    }
    
    void matrixAlloc( int** matrix, const int rows, const int cols )
    {
        matrix = new int *[ rows ];
        for ( int i = 0; i < rows; i++ )
        {
            matrix[ i ] = new int[ cols ];
            for ( int j = 0; j < cols; j++ )
            {
                matrix[ i ][ j ] = (rand() * 12347) % 10;
            }
    
        }
    }
    
    void matrixDealloc( int** matrix, const int rows )
    {       
        for ( int i = 0; i < rows; i++ )
        {
            delete [] matrix[ i ];
        }
        delete [] matrix;
    }
    
    void matrixPrint( const int* const * const matrix, const int rows, const int cols )
    {
        for ( int i = 0; i < rows; i++ )
        {
            for ( int j = 0; j < cols; j++ )
            {
                cout << matrix[ i ][ j ] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   Clark Gaebel    15 年前

    您正在按值传递双指针“matrix2”。因此,当matrixalloc完成它的任务时,“matrix2”仍然是函数被调用之前的内容。为了获得要填充的更改,请考虑通过引用传递matrix2:

    int** matrix2 = NULL;
    matrixAlloc(&matrix2, 4, 5);
    ...
    

    必要时,不要忘记将matrixalloc的实现更改为取消引用matrix2。

    EDIT: Simple solution below. Change this line:

    void matrixAlloc( int** matrix, const int rows, const int cols )
    

    对此:

    void matrixAlloc( int**& matrix, const int rows, const int cols )
    
        2
  •  1
  •   fredoverflow    15 年前
    matrixAlloc( matrix2, 4, 5 );
    

    Here you are passing matrix2 按价值 .

    void matrixAlloc( int** matrix, const int rows, const int cols )
    {
        matrix = new int *[ rows ];
    

    在这里,您要分配给一个形式参数。您传入的实际参数不受此影响。您可能应该通过引用传递参数:

    void matrixAlloc( int**& matrix, const int rows, const int cols )