抛出异常:读取访问冲突。 **dynamicArray** 是 0x1118235。发生了

Exception thrown: read access violation. **dynamicArray** was 0x1118235. occurred

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int **dynamicArray ;
int ROWS, COLUMNS;

//---------------------------------
int input_matrix(int ROWS, int COLUMNS)
{

    //---------------------------------------
    //memory allocated for elements of rows.
    int **dynamicArray = new int *[ROWS];

    //memory allocated for  elements of each column.
    for (int i = 0; i < ROWS; i++)
        dynamicArray[i] = new int [COLUMNS];

    //free the allocated memory
    for (int i = 0; i < ROWS; i++)
        delete[] dynamicArray[i];
    delete[] dynamicArray;
    //-------------------------------------

    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cin >> dynamicArray[i][j];
        }
    }
    return 0;
}
//---------------------------------------------
int print_matrix(int **Array)
{
    for (int k = 0; k < ROWS; k++)
    {
        for (int m = 0; m < COLUMNS; m++)
        {
            cout << Array[k][m];
            if (m == COLUMNS)
            {
                cout << "\n";
            }
        }
    }

    return 0;

}

//---------------------------------
int main()
{
    cin >> ROWS;
    cin >> COLUMNS;
    input_matrix(ROWS, COLUMNS);
    print_matrix(dynamicArray);

}

此代码定义了一个矩阵并获取输入并将它们放入矩阵的成员中,但每次我 运行 此代码时,我都会在行上收到读取访问冲突错误:

cin >> dynamicArray[i][j];

这里是完整的细节: 抛出异常:读取访问冲突。 dynamicArray 是 0x1118235。发生

我该怎么办?

提前谢谢你。

在这种情况下,没有理由手动进行内存管理。使用 std::vector (这是一个动态数组),甚至使用实际的 Matrix 库,例如 'Eigen'.

您的程序存在多个问题。让我一一列举。

  1. 如评论之一所述,您立即 分配内存后立即释放内存。绝对是这个 将导致分段错误或内存访问冲突时 您访问释放的内存。
  2. 当你分配内存时,你不是 将分配的内存指针分配给全局指针 dynamicArray 相反,您正在创建一个局部变量 函数内同名 input_matrix。作为这个指针 变量作用域在你失去记忆的函数内结束 分配。因此,您将再次面临分段错误或内存 print_matrix 函数内的访问冲突。
  3. 在内部 for 循环的 print_matrix 函数中,您正在检查 m==COLUMNS 是否打印新行,这永远不会发生,因为 m 总是小于 COLUMNS.
  4. 最后,正如前面的答案所暗示的那样,当您使用 C++ 时,使用带有智能指针的向量比使用数组和原始指针更好地进行内存管理是更好的选择。

以下代码片段解决了这些问题。

#include <iostream>
#include <string>
using namespace std;
int **dynamicArray ;
int ROWS, COLUMNS;

//---------------------------------
int input_matrix(int ROWS, int COLUMNS)
{
    //---------------------------------------
    //memory allocated for elements of rows.
    dynamicArray = new int *[ROWS];

    //memory allocated for  elements of each column.
    for (int i = 0; i < ROWS; i++)
        dynamicArray[i] = new int [COLUMNS];

//    cout<<"Input array values\n";

    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cin>>dynamicArray[i][j];
        }
    }
    return 0;
}

void free_matrix_memory()
{
    cout<<"freeing allocated memory\n";
    //free the allocated memory
    for (int i = 0; i < ROWS; i++)
        delete[] dynamicArray[i];
    delete[] dynamicArray;
    //-------------------------------------
}

//---------------------------------------------
int print_matrix(int **Array)
{
    cout<<"printing matrix\n";
    for (int k = 0; k < ROWS; k++)
    {
        for (int m = 0; m < COLUMNS; m++)
            cout << Array[k][m];
        cout << "\n";
    }
    return 0;
}

//---------------------------------
int main()
{
    cout<<"Row and column values\n";
    cin>> ROWS;
    cin>> COLUMNS;
    input_matrix(ROWS, COLUMNS);
    print_matrix(dynamicArray);
    free_matrix_memory();
}

还有很多可以为您做的改进,例如避免全局变量等,我将这些改进留给您。

如果你运行 Valgrind 下的程序,它会告诉你到底出了什么问题:

==6939== Invalid read of size 8
==6939==    at 0x1092C9: input_matrix(int, int) (53083248.cpp:30)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Address 0x4d7ecc0 is 0 bytes inside a block of size 16 free'd
==6939==    at 0x48373EB: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x109296: input_matrix(int, int) (53083248.cpp:23)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Block was alloc'd at
==6939==    at 0x483654F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x1091D3: input_matrix(int, int) (53083248.cpp:14)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939== 
==6939== Invalid write of size 4
==6939==    at 0x496FFF0: std::istream::operator>>(int&) (istream.tcc:194)
==6939==    by 0x1092EA: input_matrix(int, int) (53083248.cpp:30)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Address 0x4d7ed10 is 0 bytes inside a block of size 8 free'd
==6939==    at 0x48373EB: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x10927D: input_matrix(int, int) (53083248.cpp:22)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Block was alloc'd at
==6939==    at 0x483654F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x10922A: input_matrix(int, int) (53083248.cpp:18)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939== 
==6939== Invalid read of size 8
==6939==    at 0x10934D: print_matrix(int**) (53083248.cpp:42)
==6939==    by 0x10940C: main (53083248.cpp:60)
==6939==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==6939== 
==6939== 
==6939== Process terminating with default action of signal 11 (SIGSEGV)
==6939==  Access not within mapped region at address 0x0
==6939==    at 0x10934D: print_matrix(int**) (53083248.cpp:42)
==6939==    by 0x10940C: main (53083248.cpp:60)

对已释放的内存进行读取和写入,并取消引用从未分配的 dynamicArray

要解决这些问题,您需要确保您的内存寿命适合您使用它们的时间,并避免在 input_matrix().

内隐藏 dynamicArray

我建议你避免原始 new[]delete[] - 更喜欢使用容器和智能指针,它们为你拥有资源,并自动从它们的析构函数中释放它。