抛出异常:读取访问冲突.**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 << "
";
}
}
}
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 总是小于列
. - 最后,正如前面的答案所暗示的,当您使用 C++ 时,为了更好的内存管理,使用带有智能指针的向量比使用数组和原始指针更好.
- 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.
- 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 functioninput_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 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
";
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
";
//free the allocated memory
for (int i = 0; i < ROWS; i++)
delete[] dynamicArray[i];
delete[] dynamicArray;
//-------------------------------------
}
//---------------------------------------------
int print_matrix(int **Array)
{
cout<<"printing matrix
";
for (int k = 0; k < ROWS; k++)
{
for (int m = 0; m < COLUMNS; m++)
cout << Array[k][m];
cout << "
";
}
return 0;
}
//---------------------------------
int main()
{
cout<<"Row and column values
";
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屋!