我有VC ++的问题,简单来说,我讨厌它哈哈。我的代码似乎在Mac上运行正常,但是当我尝试在VC ++中运行它时,在调试中出现此错误:


  Windows已在Assignment1-FINAL.exe中触发一个断点。
  
  这可能是由于堆损坏所致,这表明存在错误
  Assignment1-FINAL.exe或它已加载的任何DLL。
  
  这也可能是由于用户在按下
  Assignment1-FINAL.exe具有焦点。


我知道我没有按F12,所以我不确定为什么会得到这个……然后,当我尝试以发布模式运行它时,我得到了:


  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;

最佳答案

初始想法:
断言在哪里?您在countPointsAboveThreshold()中作为数组访问的Points *,但完全不进行边界检查以确保您未通过数组的末尾。这将是我检查内存重载操作的第一个区域。同样,直接指针调用非常C。哎呀,您无需检查任何数组调用中的边界。危险的

长度为0的新数组可能是安全的,也可能是不安全的。我会小心的。

每当我在陈述中看到[i-1]时都会感到紧张。在i == 0时非常容易读取垃圾

i,j,k循环与四重嵌套ifs混合在一起并持续并中断吗?不。重新考虑一下这种逻辑。是这样,WAY太复杂了。

您即将返回,并在pointsToCalculate []中分配了内存。那里的内存泄漏。

我可能建议将您的最后一个功能分成多个部分以简化逻辑?

我讨厌K&R样式的括号。不过,您可以选择-不是在这里展开这场圣战:P

除此之外,我会提出我的第一个建议,并确保始终设置您的最终bool,并且您不会超出范围。如前所述,stl :: vector和一些引用(最好是const)是您的朋友。

09-05 06:42