引发异常:读取访问冲突。 ** dynamicArray **为0x1118235。发生

时间:2018-10-31 12:22:22

标签: c++ matrix dynamic-memory-allocation access-violation

#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。发生

我该怎么办?

谢谢。

3 个答案:

答案 0 :(得分:2)

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

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

仍然可以为您做很多改进,例如避免使用全局变量等。我将由您自己来进行这些改进。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

如果您在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的取消引用。

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

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