引发异常:读取访问冲突. ** dynamicArray **为0x1118235.发生 [英] Exception thrown: read access violation. **dynamicArray** was 0x1118235. occurred

查看:137
本文介绍了引发异常:读取访问冲突. ** dynamicArray **为0x1118235.发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}

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

This code defines a matrix and get inputs and puts them in the members of the matrix but Every time I run this code I get read access violation error on the line:

cin >> dynamicArray[i][j];

这里是完整的详细信息: 引发异常:读取访问冲突. dynamicArray 为0x1118235.发生

here are the full details: Exception thrown: read access violation. dynamicArray was 0x1118235. occurred

我该怎么办?

谢谢.

推荐答案

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

There are multiple issues with your program. Let me list all of them one by one.

  1. 如其中一条评论中所述,您将立即 分配内存后立即取消分配内存.绝对是这个 在以下情况下将导致分段错误或内存访问冲突: 您访问已释放的内存.
  2. 分配内存时,您不是 将分配的内存指针分配给全局指针 dynamicArray而是使用 函数input_matrix中的相同名称.作为这个指针 变量作用域在丢失内存的函数内结束 已分配.因此,您将再次遇到细分错误或内存 print_matrix函数内部的访问冲突.
  3. 在内部for循环中的
  4. 内部print_matrix函数中,您正在检查m==COLUMNS是否要打印新行,因为m总是小于COLUMNS,所以这永远不会发生.
  5. 最后,正如前面的答案所暗示的,当您使用C ++时,使用带有智能指针的向量比使用数组和原始指针更好地进行内存管理是更好的选择.
  1. As mentioned in one of the comments, You are immediately deallocating memory just after you allocated it. Definitely this will result in a segmentation fault or memory access violation when you access deallocated memory.
  2. When you allocate the memory you are not assigning the allocated memory pointers to global pointer dynamicArray instead you are creating a local variable with the same name inside the function input_matrix. As this pointer variable scope ends inside the function you are losing the memory allocated. Hence again you will face segmentation fault or memory access violation inside print_matrix function.
  3. Inside print_matrix function in inner for loop you are checking if m==COLUMNS to print new line, this will never happen since m is always less than COLUMNS.
  4. Finally, as the previous answer suggests when you are using C++, using a vector with smart pointers is a better choice than using array and raw pointers for better memory management.

以下代码片段可以解决这些问题.

Following snippet resolves those issues.

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

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

Still many improvements can be done for your such as avoiding global variables etc., I am leaving it up to you to do those improvements.

这篇关于引发异常:读取访问冲突. ** dynamicArray **为0x1118235.发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆