C++ int值从0跳到32679,没有变化
我有一个非常非常奇怪的问题,我就是想不出来。所以你可以看看,这是我的代码C++ int值从0跳到32679,没有变化,c++,variables,loops,int,C++,Variables,Loops,Int,我有一个非常非常奇怪的问题,我就是想不出来。所以你可以看看,这是我的代码 point * findLongPaths(point * points, double threshold_distance) { int i = 0; int pointsAboveThreshold = countPointsAboveThreshold(points, threshold_distance); point * pointsByThreshold = new point[si
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
为什么会这样?这真的很奇怪,我不知道如何修复它?我注意到一些事情可能会有所帮助:
几乎肯定是错误的,因为newpoint[sizeof(points)]
不等于数组中的元素数,而是指针的大小(通常为4或8)。如果您想要sizeof(points)
的大小,可以将其作为另一个参数传递到函数中,或者更好地使用标准容器(如点[]
或任何适合您需要的容器)std::vector
- 您的
数组分配了pointsToCalculate
元素,但您随后使用pointsAboveThreshold
访问它。如果i
曾经超过i
(几乎肯定会这样),您将使数组溢出,并会发生不好的事情,可能包括覆盖pointsAboveThreshold
。如果没有更多的信息和细节,我怀疑这是你的问题i
- 当
和distance>threshold\u distance
时,您将pointTwo.end==false
增加两次(这可能是有意的,但希望在案例中提及)李>i
永远不能像外部else if(points[0].end==true…)
将为false,并且不会进入循环。我不确定您的预期逻辑,但我怀疑您希望将其置于while循环之外一样为true,而(points[i].end!=true)
i
去哪里?(我想,i++
就在while循环中)你把断点放在哪里了?看起来您的points数组有32680个条目,没有任何end
-值设置为true。是的,i
在while循环中递增。我将断点放在注释行//找到距离最小的点”,并将I=0`移动到int locationToStore=0
行下方。我可能会去看一看,看能不能找到类似的东西。你可能是误算了某个数组的大小,并在分配的区域之外写入了内容。例如point*pointsByThreshold=新点[sizeof(points)]代码>根据指针的大小分配4或8个点。可能不是预期的。//如果已经计算了最后一个点,如果(pointTwo.end==true){pointsToCalculate[i].end=true;break;}否则{pointsToCalculate[i].end=false;i++;}则中断循环
这似乎打破了循环,给出了计算点的最终值。end
为true
…啊哈。。。。“非常正确,”博乌·佩尔松说。我想解决这个问题的方法是创建一个返回int的函数。该函数将包含一个循环,如果存在点的值,则在没有更多点时递增并返回递增的值?然后,我可以用返回的值创建pointsByThreshold
?你觉得这样行吗?