【问题标题】:int value jumps from 0 to 32679 without changeint 值从 0 跳转到 32679 没有变化
【发布时间】: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 moved i = 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 吗?你认为这可行吗?

标签: c++ variables loops int


【解决方案1】:

我注意到的几件事可能会有所帮助:

  • new point[sizeof(points)] 几乎肯定是错误的,因为sizeof(points) 不等于数组中的元素数量,而是指针的大小(通常为 4 或 8)。如果您想要 points[] 的大小,可以将其作为另一个参数传递给函数,或者更好的是,使用标准容器(如 std::vector<> 或任何适合您需要的容器)。
  • 您的pointsToCalculate 数组分配有pointsAboveThreshold 元素,但您随后使用i 访问它。如果i 曾经超过pointsAboveThreshold(几乎肯定会),您将溢出数组并且会发生坏事,可能包括覆盖i。如果没有更多信息和细节,我会怀疑这是您的问题。
  • distance > threshold_distancepointTwo.end == false 增加两次i 时(这可能是有意的,但想以防万一)。
  • else if (points[0].end == true...) 永远不会为真,就好像外部的while (points[i].end != true) 将是假的,循环永远不会进入。我不确定您的预期逻辑,但我怀疑您希望在 while 循环之外这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多