问题描述
#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.
- 如其中一条评论中所述,您将立即分配内存后立即取消分配内存.绝对是这个在以下情况下将导致分段错误或内存访问冲突:您访问已释放的内存.
- 分配内存时,您不是将分配的内存指针分配给全局指针
dynamicArray
而是使用函数input_matrix
中的相同名称.作为这个指针变量作用域在丢失内存的函数内结束已分配.因此,您将再次遇到细分错误或内存print_matrix
函数内部的访问冲突. 在内部for循环中的 - 内部
print_matrix
函数中,您正在检查m==COLUMNS
是否要打印新行,因为m总是小于COLUMNS
,所以这永远不会发生. - 最后,正如前面的答案所暗示的,当您使用C ++时,使用带有智能指针的向量比使用数组和原始指针更好地进行内存管理是更好的选择.
- As mentioned in one of the comments, You are immediatelydeallocating memory just after you allocated it. Definitely thiswill result in a segmentation fault or memory access violation whenyou access deallocated memory.
- When you allocate the memory you are notassigning the allocated memory pointers to global pointer
dynamicArray
instead you are creating a local variable with thesame name inside the functioninput_matrix
. As this pointervariable scope ends inside the function you are losing the memoryallocated. Hence again you will face segmentation fault or memoryaccess violation insideprint_matrix
function. - Inside
print_matrix
function in inner for loop you are checking ifm==COLUMNS
to print new line, this will never happen since m is always less thanCOLUMNS
. - 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.发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!