【发布时间】:2011-10-31 05:53:38
【问题描述】:
我有一个非常非常奇怪的问题,我就是想不通。所以你可以看看,这是我的代码;
point * findLongPaths(point * points, double threshold_distance) {
int i = 0;
int pointsAboveThreshold = countPointsAboveThreshold(points, threshold_distance);
point * pointsByThreshold = new point[sizeof(points)];
pointValues * pointsToCalculate = new pointValues[pointsAboveThreshold];
//pointValues pointsToCalculate[pointsAboveThreshold];
//point orderedPoints[pointsAboveThreshold];
while (points[i].end != true) {
point pointOne = points[i];
point pointTwo = points[i + 1];
//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].originalLocation = i;
pointsToCalculate[i].distance = distance;
pointsToCalculate[i].final = pointTwo;
//If the final point has been calculated, break the loop
if (pointTwo.end == true) {
pointsToCalculate[i].end = true;
break;
} else {
pointsToCalculate[i].end = false;
i++;
}
} else if (points[0].end == true || pointsAboveThreshold == 0) {
//If there is no points above the threshold, return an empty point
if (points[0].end == true) {
point emptyPoint;
emptyPoint.x = 0.0;
emptyPoint.y = 0.0;
emptyPoint.end = true;
pointsByThreshold[0] = emptyPoint;
return pointsByThreshold;
}
}
i++;
}
i = 0;
//Find the point with the lowest distance
int locationToStore = 0;
while (pointsToCalculate[i].end != true) {
我的问题是,i 值实际上是从 0 到 32679。我最初将它设置为 j,所以它使用了与之前 while 循环中的计数器不同的计数器,但我尝试使用 i 来看看会不会有什么不同。
我已经在 VC++ 和 XCode 中尝试过,并且都在这样做。但是,如果我在它前面几行放置一个断点,它就会保持为零。如果我在没有任何断点的情况下运行它,它会将值更改为 32679。
这是为什么?这真的很奇怪,我不知道如何解决它?
【问题讨论】:
-
你
i上哪儿去了? (我想i++就在while 循环内)你把断点放在哪里了?看起来你的点数组有 32680 个条目,没有任何end-value 设置为 true。 -
是的,
i在 while 循环中递增。我将断点放在int locationToStore = 0行下方的注释行//Find the points with the lowest distance' and movedi = 0`。有可能,我去看看能不能找到类似的东西。 -
您可能误算了某个数组的大小,并在分配区域之外写入。例如,
point * pointsByThreshold = new point[sizeof(points)];根据指针的大小分配 4 或 8 个点。可能不是预期的结果。 -
//If the final point has been calculated, break the loop if (pointTwo.end == true) { pointsToCalculate[i].end = true; break; } else { pointsToCalculate[i].end = false; i++; }这似乎打破了循环并将pointsToCalculate.end的最终值设为true... -
啊……非常真实,@Bo_Persson。我想一种解决方法是创建一个返回 int 的函数。该函数将包含一个循环,如果存在点的值,则在没有更多点时递增并返回递增的值?那么,我可以用返回值创建
pointsByThreshold吗?你认为这可行吗?