在 Project1CSCI115.exe 中的 0x7BC9E829 (ucrtbased.dll) 抛出异常:0xC0000005:访问冲突读取位置 0x80A801E4
Exception thrown at 0x7BC9E829 (ucrtbased.dll) in Project1CSCI115.exe: 0xC0000005: Access violation reading location 0x80A801E4
我正在编写一些代码,将邻接矩阵转换为邻接列表,运行 转换为问题。我将其追溯到内存错误,但是当我使用我的调试器单步执行时,它直接忽略了错误,并且没有抛出异常。当程序在没有断点的情况下运行时,它会抛出异常。我感觉这是由于堆损坏造成的,但我不是专家。
#include <string>
#include <limits>
#include <iostream>
using namespace std;
class NodeAM {
public:
NodeAM() : v(0), weight(0), next(nullptr) {}
NodeAM(int v1, double weight1) : v(v1), weight(weight1), next(nullptr) {}
~NodeAM() {}
int v;
double weight;
NodeAM* next;
};
class matrix {
public:
matrix();
matrix(int m, int n);
~matrix();
void setCurrentVertex(int u);
void setAdjacencyMatrix(map* mapMatrix);
void setDirectEdge(int u, int v, double w);
private:
NodeAM** aMatrix;
NodeAM** curr;
int numVertices;
int infinity = std::numeric_limits<int>::max(); //displayed as INF
};
matrix::matrix() {
numVertices = 0;
}
matrix::matrix(int m, int n) {
numVertices = m * n;
aMatrix = new NodeAM * [n]; //n is columns
curr = new NodeAM * [n];
for (int i = 0; i < n; ++i) {
aMatrix[i] = nullptr;
curr[i] = nullptr;
}
}
matrix::~matrix() {
for (int i = 0; i < numVertices; ++i) {
NodeAM* curr = aMatrix[i];
while (curr) {
NodeAM* next = curr->next;
delete curr;
curr = next;
}
}
delete[] aMatrix;
delete[] curr;
}
void matrix::setAdjacencyMatrix(map* mapMatrix) {
double weightArr;
for (int i = 0; i < numVertices; ++i) {
for (int j = 0; j < numVertices; ++j) {
if (mapMatrix->valueAt(i, j) != infinity) {
weightArr = mapMatrix->valueAt(i, j);
setDirectEdge(i, j, weightArr);
}
}
}
}
void matrix::setDirectEdge(int u, int v, double w) {
NodeAM* tmp = new NodeAM(v, w); //exception thrown when u is 22 and v is 23
tmp->next = aMatrix[u];
aMatrix[u] = tmp;
}
double map::valueAt(int row, int cols) {
return adjMatrixCopy[row][cols];
}
/*
for the sake of not posting tons of code, adjMatrixCopy is an adjacency
matrix, which either has infinity as the edge or the edge weight (1, 4, 7, etc.) as
the edge, like this:
INF, INF, 7, 3, 7
INF, INF, 3, 7, 7
1, 9999, INF, 7, 3
In adjMatrixCopy, it is a 50 x 50 matrix similar to the one above
*/
构造函数应该是
matrix::matrix() {
numVertices = 0;
}
matrix::matrix(int m, int n) {
numVertices = m * n;
aMatrix = new NodeAM * [n*m]; //n is columns
curr = new NodeAM * [n];
currVert = new int[n];
for (int i = 0; i < n; ++i) {
aMatrix[i] = nullptr;
curr[i] = nullptr;
}
}
在aMatrix = new NodeAM* [n*m]处,数组应该是邻接矩阵的大小,以防它必须存储矩阵的每个值。这就是它抛出异常的原因,因为它没有足够的内存来保存邻接矩阵的值。
我正在编写一些代码,将邻接矩阵转换为邻接列表,运行 转换为问题。我将其追溯到内存错误,但是当我使用我的调试器单步执行时,它直接忽略了错误,并且没有抛出异常。当程序在没有断点的情况下运行时,它会抛出异常。我感觉这是由于堆损坏造成的,但我不是专家。
#include <string>
#include <limits>
#include <iostream>
using namespace std;
class NodeAM {
public:
NodeAM() : v(0), weight(0), next(nullptr) {}
NodeAM(int v1, double weight1) : v(v1), weight(weight1), next(nullptr) {}
~NodeAM() {}
int v;
double weight;
NodeAM* next;
};
class matrix {
public:
matrix();
matrix(int m, int n);
~matrix();
void setCurrentVertex(int u);
void setAdjacencyMatrix(map* mapMatrix);
void setDirectEdge(int u, int v, double w);
private:
NodeAM** aMatrix;
NodeAM** curr;
int numVertices;
int infinity = std::numeric_limits<int>::max(); //displayed as INF
};
matrix::matrix() {
numVertices = 0;
}
matrix::matrix(int m, int n) {
numVertices = m * n;
aMatrix = new NodeAM * [n]; //n is columns
curr = new NodeAM * [n];
for (int i = 0; i < n; ++i) {
aMatrix[i] = nullptr;
curr[i] = nullptr;
}
}
matrix::~matrix() {
for (int i = 0; i < numVertices; ++i) {
NodeAM* curr = aMatrix[i];
while (curr) {
NodeAM* next = curr->next;
delete curr;
curr = next;
}
}
delete[] aMatrix;
delete[] curr;
}
void matrix::setAdjacencyMatrix(map* mapMatrix) {
double weightArr;
for (int i = 0; i < numVertices; ++i) {
for (int j = 0; j < numVertices; ++j) {
if (mapMatrix->valueAt(i, j) != infinity) {
weightArr = mapMatrix->valueAt(i, j);
setDirectEdge(i, j, weightArr);
}
}
}
}
void matrix::setDirectEdge(int u, int v, double w) {
NodeAM* tmp = new NodeAM(v, w); //exception thrown when u is 22 and v is 23
tmp->next = aMatrix[u];
aMatrix[u] = tmp;
}
double map::valueAt(int row, int cols) {
return adjMatrixCopy[row][cols];
}
/*
for the sake of not posting tons of code, adjMatrixCopy is an adjacency
matrix, which either has infinity as the edge or the edge weight (1, 4, 7, etc.) as
the edge, like this:
INF, INF, 7, 3, 7
INF, INF, 3, 7, 7
1, 9999, INF, 7, 3
In adjMatrixCopy, it is a 50 x 50 matrix similar to the one above
*/
构造函数应该是
matrix::matrix() {
numVertices = 0;
}
matrix::matrix(int m, int n) {
numVertices = m * n;
aMatrix = new NodeAM * [n*m]; //n is columns
curr = new NodeAM * [n];
currVert = new int[n];
for (int i = 0; i < n; ++i) {
aMatrix[i] = nullptr;
curr[i] = nullptr;
}
}
在aMatrix = new NodeAM* [n*m]处,数组应该是邻接矩阵的大小,以防它必须存储矩阵的每个值。这就是它抛出异常的原因,因为它没有足够的内存来保存邻接矩阵的值。