二维数组分配问题

发布于 2024-09-06 23:45:02 字数 1938 浏览 0 评论 0原文

这是昨天我朋友被问到的一个面试问题。问题是这样的:这个程序是否会因“访问冲突”错误而崩溃?我看了一会儿,心想不,不会。但在视觉工作室中实际尝试证明我错了。我无法弄清楚这里发生了什么......或者更准确地说,我知道发生了什么,但不明白为什么。问题似乎是矩阵2数组根本没有被分配。

代码如下:

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

this is an interview question my friend was asked yesterday. The question was something like: will this program crash with an "access violation" error or not? I looked at it for a while and thought no, it won't. But actually trying this out in visual studio proved me wrong. I cannot figure out what happens here... or to be more precise, i know what happens, but do not understand WHY. The problem seems to be that the matrix2 array does not get allocated at all.

Code below:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺 2024-09-13 23:45:02

您正在按值传递双指针“matrix2”。因此,当matrixAlloc完成其工作时,“matrix2”仍将是调用该函数之前的值。为了填充更改,请考虑通过引用传递matrix2:

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

不要忘记在必要时更改matrixAlloc的实现以取消引用matrix2。

编辑:下面的简单解决方案。将此行:更改

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

为:

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

You are passing the double pointer "matrix2" by value. Therefore, when matrixAlloc finishes doing its thing, "matrix2" will still be whatever it was before the function was called. In order to get the change to populate, consider passing matrix2 by reference:

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

Don't forget to change the implementation of matrixAlloc to dereference matrix2 when necessary.

EDIT: Simple solution below. Change this line:

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

to this:

void matrixAlloc( int**& matrix, const int rows, const int cols )
源来凯始玺欢你 2024-09-13 23:45:02
matrixAlloc( matrix2, 4, 5 );

在这里,您按值传递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 )
matrixAlloc( matrix2, 4, 5 );

Here you are passing matrix2 by value.

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

And here, you are assigning to a formal parameter. The actual parameter that you passed in is not affected by that. You should probably pass the parameter by reference:

void matrixAlloc( int**& matrix, const int rows, const int cols )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文