访问违规阅读位置

发布于 2024-11-30 18:30:46 字数 4531 浏览 0 评论 0原文

我对 VC++ 有疑问,简单来说,我讨厌它,哈哈。我的代码似乎在我的 Mac 上运行得很好,但是当我尝试在 VC++ 中运行它时,我在调试中收到此错误:

Windows 在Assignment1-FINAL.exe 中触发了断点。

这可能是由于堆损坏造成的,这表明存在错误 Assignment1-FINAL.exe 或它已加载的任何 DLL。

这也可能是由于用户在按下 F12 的同时 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;

I have a problem with VC++, simply, I hate it haha. My code seems to be running all fine on my Mac but when I try to run it in VC++, I get this error in debug:

Windows has triggered a breakpoint in Assignment1-FINAL.exe.

This may be due to a corruption of the heap, which indicates a bug in
Assignment1-FINAL.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while
Assignment1-FINAL.exe has focus.

I know for a fact I haven't pressed F12 so I am not sure why I am getting this... Then, when I try to run it in Release mode, I get this:

Unhandled exception at 0x00401473 in Assignment1-FINAL.exe:
0xC0000005: Access violation reading location 0x00347015.

This is the code I am using:

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;
}

And this is the main function:

    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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-12-07 18:30:46

初步想法:
断言在哪里?您将 countPointsAboveThreshold() 中的 Points* 作为数组进行访问,但根本不进行边界检查以确保您没有通过数组的末尾。这将是我检查内存踩踏行为的第一个领域。另外,直接指针调用非常 C。哎呀,您没有在任何数组调用中检查边界。危险...

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

哎呀,每当我在声明中看到 [i - 1] 时,我都会感到紧张。很容易在 i == 0 i,j,k 循环中读取垃圾

,并使用四重嵌套 if 与 continue 和break 混合?不,重新思考这个逻辑。这实在是太复杂了。

您提前返回了在pointsToCalculate[] 中分配的内存。那里有内存泄漏。

我是否可以建议将您的最后一个函数分成多个部分以简化逻辑?

我讨厌 K&R 风格的括号。不过,你的选择 - 不是在这里开始这场圣战:P

除此之外,我会采纳我的第一个建议,并确保你的最终布尔值始终被设置,并且你不会超出范围。正如之前所建议的,stl::vector 和一些参考文献(最好是 const)是你的朋友。

Inital thoughts:
Where's the asserts? Your accessing Points* in countPointsAboveThreshold() as an array, but do no bounds checking at all to make sure you aren't pass the array's end. This would be my first area of checking for memory stomping action. Also, straight pointer calls are very C. Heck, you aren't check bounds in any of your array calls. Dangerous...

Newing arrays of length 0 may or may not be safe. I'd be careful of that.

Heck anytime I see [i - 1] in a statement I get nervous. Very easy to read garbage at i == 0

i,j,k loops with quadrouple nested ifs mixed with continues and a break? No. Rethink that logic. It is way, WAY too complicated.

You are early returning with memory allocated in pointsToCalculate[]. Memory leak there.

Might I suggest breaking your last function into multiple parts to simplify the logic?

Man I hate K&R style brackets. Your choice though - not here to start that holy war :P

Beyond that, I'd go with my first suggestion and make sure that your end bool is set always and that you aren't going out of bounds. As previously suggested, stl::vector and a few references (preferably const) are your friend here.

×纯※雪 2024-12-07 18:30:46

您将其发布为 C++,但它似乎只使用了很少的 C++ 实际内容:对象。这段代码读起来更像 C。

只是一些注意事项:

  1. 使用 C++,您不需要执行 typedef struct {...} point,而是执行 struct point {...} code> 做你想做的事。
  2. 如果您使用 stl::vector 而不是 c 数组,那么您的循环将变得更加简单,您将不再需要函数 totalPoints()。您还可以从 pointpointValues 中删除成员变量 end
  3. 您在堆上而不是在堆栈上创建了很多变量没有充分的理由。使用 stl::vector (或其他标准容器)、局部变量和引用,您可以极大地简化内存管理并避免诸如此类的奇怪崩溃。

我将更深入地研究您的代码,看看是否可以为您提供一些更具体的指导,但您确实应该进一步阅读 C++ 相对于 C 提供的内容。我会看一下 cplusplus.comC++ 常见问题解答此处还提供了一些优秀的书籍建议。

You posted this as C++ but it seems to be using very little of what C++ actually is all about: objects. This code reads much more like C.

Just some notes:

  1. With C++ you don't need to do typedef struct {...} point, doing struct point {...} does what you are trying to do.
  2. If you use a stl::vector instead of a c-array then your loops will become much simpler and you won't need your function totalPoints(). You can also get rid of the member variable end from point and pointValues
  3. You are creating a lot of variables on the heap rather than on the stack for no good reason. With stl::vector (or other standard containers), local variables, and references you can greatly simplify your memory management and avoid strange crashes such as these.

I'll take a deeper look at your code and see if I can give you some more specific guidance but you really should do some further reading into what C++ provides over C. I'd take a look at cplusplus.com and the C++ FAQ. There are also some excellent book suggestions here.

远昼 2024-12-07 18:30:46

这部分代码对我来说听起来很奇怪:

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;
...

我认为您需要使用另一个索引变量(i - 1 除外)来填充pointsToCalculate!

我会重写这一部分,如下所示:

int i = 1;
int index = 0;

// if points[i - 1].end is true how you could access points[i] ?
while (points[i].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[index].originalLocation = i - 1;
        pointsToCalculate[index].distance = distance;
        pointsToCalculate[index].final = pointTwo;
        pointsToCalculate[index].stored = false;

        ++ index;
    }

    ++i;
}

pointsToCalculate[index].end = true;

** 另请注意,您的数组中至少需要两个点,否则您会再次遇到访问冲突,因此您需要检查这一点,并且在“countPointsAboveThreshold”函数中遇到与您相同的问题也需要修复。

请检查语法和拼写错误;)

但无论如何,我也强烈建议遵循最后两个帖子的建议。

This part of your code sounds odd to me:

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;
...

I think you need to use another index variable (other than i - 1) to populate pointsToCalculate!

I would rewrite this part something like this:

int i = 1;
int index = 0;

// if points[i - 1].end is true how you could access points[i] ?
while (points[i].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[index].originalLocation = i - 1;
        pointsToCalculate[index].distance = distance;
        pointsToCalculate[index].final = pointTwo;
        pointsToCalculate[index].stored = false;

        ++ index;
    }

    ++i;
}

pointsToCalculate[index].end = true;

** Also note that you need at least two points in your array or you get access violation again, so you need to check for this and you have the same problem in "countPointsAboveThreshold" function that you need to fix too.

Please check for syntax and typos ;)

But any way I strongly recommend following two last post recommendations too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文