我在Visual Studio Express 2013中编写了一个Windows控制台应用程序。它在debugging模式下编译并运行正常,但是发行版本崩溃( 访问冲突读取位置0xFFFFFFFF )。 在stackoverflow上有几个类似的问题,但他们似乎没有解决我的问题。 它们通常与未初始化的variables相关, 访问超出数组边界的元素 , 指针算术或dynamic分配的内存 。 我不认为这些适用于这里,但我想被certificate是错误的。
我大大减less了代码(原来是1000到2000行之间),每次删除代码并检查它是否仍然崩溃。 我似乎无法让它变小,仍然有错误显示。 现在代码非常简单,我不知道错误在哪里。 这可能是一个标准的C + +错误(我对这个语言很新鲜)。 我不想问这样一个通用的问题,但是:
以下代码中的错误在哪里?
现在我几乎所有的东西都会让bug消失。 当我禁用优化时(甚至当我优化,但禁用内联),它也消失。 我唯一的怀疑就是它可能与vectorvector有关(left_neighbors)。
Point.h
#pragma once class Point { public: double x, y; Point(double xcoord, double ycoord); };
Point.cpp
#include "stdafx.h" #include "Point.h" Point::Point(double xcoord, double ycoord) { x = xcoord; y = ycoord; }
Diagram.h
#pragma once #include "Point.h" #include <vector> class Diagram { public: void check_for_crossing(); std::vector<Point> vertices; std::vector<std::vector<size_t>> left_neighbors; };
Diagram.cpp
#include "stdafx.h" #include "Diagram.h" double y_coordinate_of_segment_at(const Point start, const Point end, const double x) { return (start.y + (x - start.x) / (end.x - start.x) * (end.y - start.y)); } void Diagram::check_for_crossing() { Point end1 = Point(1.5, 0.2); Point end2 = Point(2.8, 3.4); double y1_at_min = y_coordinate_of_segment_at(Point(0.5, 0.5), end1, 0.5); double y2_at_min = y_coordinate_of_segment_at(Point(1.5, 0.2), end2, 0.5); Point intersection(0.0, 0.0); intersection.x = (y2_at_min - y1_at_min) / (y2_at_min); // y2_at_min is not 0 intersection.y = y_coordinate_of_segment_at(Point(0.5, 0.5), end1, intersection.x); vertices.push_back(intersection); left_neighbors.push_back({ 0, 1 }); }
Berlin.cpp (这是主函数所在的地方)
#include "stdafx.h" #include <tchar.h> #include "Point.h" #include "Diagram.h" Diagram create_diagram() { Diagram diagram; diagram.vertices.push_back(Point(2.8, 3.4)); return diagram; } int _tmain(int argc, _TCHAR* argv[]) { Diagram diag = create_diagram(); diag.check_for_crossing(); return 0; }
该项目中唯一的其他文件是stdafx.h和stdafx.cpp ,用于预编译头文件。 stdafx.cpp的内容只有#include“stdafx.h” ,而stdafx.h是空的。