开发者

int value jumps from 0 to 32679 without change

开发者 https://www.devze.com 2023-03-29 06:17 出处:网络
I have a really, really weird problem that I just can\'t figure out. So you can have a look, here is my code;

I have a really, really weird problem that I just can't figure out. So you can have a look, here is my code;

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 开发者_C百科pointsByThreshold;
            }
        }
        i++;
    }
    i = 0;

    //Find the point with the lowest distance
    int locationToStore = 0;

    while (pointsToCalculate[i].end != true) {

My problem is, the i value literally goes from 0 to 32679. I originally had it set to j, so it was using a different counter to the one in the while loop before but I tried it with the i to see if it would make a difference.

I have tried it in both VC++ and XCode and both are doing it. However, if I place a breakpoint a few lines before it, it stays as a zero. If I run it without any breakpoints, it changes the value to 32679.

Why is this? It's really weird and I have no idea how to fix it?


A few things I noticed that may help:

  • The new point[sizeof(points)] is almost certainly wrong as sizeof(points) does not equal the number of elements in the array but is the size of the pointer (usually 4 or 8). If you want the size of points[] either pass it into the function as another parameter or, even better, use a standard container (like std::vector<> or whichever one fits your needs).
  • Your pointsToCalculate array is allocated with pointsAboveThreshold elements but you then access it with i. If i ever exceeds pointsAboveThreshold (which almost certainly will) you will overflow the array and bad things will happen, possible including overwriting i. I would suspect this is your issue without more information and detail.
  • When distance > threshold_distance and pointTwo.end == false you increment i twice (this may be intended but wanted to mention it in case).
  • The else if (points[0].end == true...) can never be true as if it was the outer while (points[i].end != true) would be false and the loop never entered. I'm unsure of your intended logic but I would suspect you want this outside of the while loop.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号