【发布时间】:2011-08-21 05:21:53
【问题描述】:
我对 VC++ 有问题,简单地说,我讨厌它哈哈。我的代码在我的 Mac 上似乎运行良好,但是当我尝试在 VC++ 中运行它时,我在调试时收到此错误:
Windows 已在 Assignment1-FINAL.exe 中触发断点。
这可能是由于堆损坏,这表明存在错误 Assignment1-FINAL.exe 或它已加载的任何 DLL。
这也可能是由于用户在按下 F12 的同时 Assignment1-FINAL.exe 有焦点。
我知道我没有按 F12,所以我不确定我为什么会得到这个...然后,当我尝试在 Release 模式下运行它时,我得到这个:
Assignment1-FINAL.exe 中 0x00401473 处的未处理异常: 0xC0000005:访问冲突读取位置0x00347015。
这是我正在使用的代码:
int countPointsAboveThreshold(point * points, double threshold_distance) {
int i = 1;
int count = 0;
while (points[i - 1].end != true) {
point pointOne = points[i -1];
point pointTwo = points[i];
double distance = distanceBetweenTwoPoints(pointOne, pointTwo);
if (pointTwo.end == true) {
if (distance > threshold_distance) {
count++;
return count;
} else {
return count;
}
} else if (distance > threshold_distance) {
count++;
}
i++;
}
return count;
}
int totalPoints(point * points) {
int i = 0;
while (points[i].end != true) {
i++;
}
return i + 1;
}
point * findLongPaths(point * points, double threshold_distance) {
int i = 1;
int locationToStore = 0;
int pointsAboveThreshold = countPointsAboveThreshold(points, threshold_distance);
point * pointsByThreshold = new point[pointsAboveThreshold];
pointValues * pointsToCalculate = new pointValues[pointsAboveThreshold];
while (points[i - 1].end != true && i < pointsAboveThreshold) {
point pointOne = points[i - 1];
point pointTwo = points[i];
//Check to see if the distance is greater than the threshold, if it is store in an array of pointValues
double distance = distanceBetweenTwoPoints(pointOne, pointTwo);
if (distance > threshold_distance) {
pointsToCalculate[i - 1].originalLocation = i - 1;
pointsToCalculate[i - 1].distance = distance;
pointsToCalculate[i - 1].final = pointTwo;
pointsToCalculate[i - 1].stored = false;
//If the final point has been calculated, break the loop
if (pointTwo.end == true) {
pointsToCalculate[i].end = true;
break;
} else {
pointsToCalculate[i - 1].end = false;
i++;
continue;
}
}
}
if (points[0].end == true && pointsAboveThreshold == 0) {
point emptyPoint;
emptyPoint.x = 0.0;
emptyPoint.y = 0.0;
emptyPoint.end = true;
pointsByThreshold[0] = emptyPoint;
return pointsByThreshold;
}
//Find the point with the lowest distance
int j = 2;
//EDITED
pointValues pointWithLowest;
pointWithLowest = pointsToCalculate[0];
while (pointsToCalculate[j - 1].end != true) {
for (int k = 1; pointsToCalculate[k - 1].end != true; k++) {
if (pointsToCalculate[k - 1].stored == true) {
k++;
continue;
} else {
if (pointsToCalculate[k - 1].distance > pointWithLowest.distance) {
pointWithLowest = pointsToCalculate[k - 1];
k++;
continue;
} else if (pointsToCalculate[k - 1].distance == pointWithLowest.distance) {
if (pointWithLowest.originalLocation < pointsToCalculate[k - 1].originalLocation) {
pointWithLowest = pointsToCalculate[k - 1];
k++;
continue;
} else {
k++;
continue;
}
} else {
pointWithLowest.stored = true;
pointsByThreshold[locationToStore] = pointWithLowest.final;
locationToStore++;
break;
}
}
}
//DEBUGGER STOPS HERE
j++;
}
delete[] pointsToCalculate;
return pointsByThreshold;
}
这是主要功能:
point *longest_calculated = findLongPaths(p, 1.1);
std::cout << "Should equal " << longest[1].y << ": " << longest_calculated[1].y;
delete longest_calculated;
cin.get();
return 0;
【问题讨论】:
-
那么调试器在代码的什么地方停止了?
-
我加了一条评论,快到结尾了
j++; -
这里发生了很多事情——你能发布
point的定义吗? -
typedef struct { int x; int y; bool end; } point; typedef struct { int originalLocation; double distance; point final; bool stored; bool end; } pointValues;有point和pointValus的定义 -
我无法遵循您的代码,因此很难提供建议。但是通过带有一些组成值的调试器运行,我在最后一个 for 循环中遇到了访问冲突。 j 和 locationToStore 的值不断上升,直到我写到我分配的数组的边界之外。显然算法是错误的,但由于我无法遵循它,因此很难建议如何改进它。
标签: c++ loops exception dynamic heap-memory