I am writing a Windows console application in Visual Studio Express 2013. It compiles and works fine in debug mode, but the version version fails (access to error detection address 0xFFFFFFFF). There are a few similar questions about stackoverflow, but none of them seem to solve my problem. They are usually associated with uninitialized variables , access to elements outside the array , arithmetic pointer, or dynamically allocated memory strong>. I do not think that any of them is applicable here, but I would like to be proved incorrectly.
I significantly reduced the code (the original ranged from 1000 to 2000 lines), each time deleting the code and checking that it still crashes. It seems I can not make it smaller and there is still an error. Now that the code is so simple, I don’t understand where the error is. This is probably a standard C ++ bug (I'm pretty new to the language). I don't like to ask such a general question, but:
Where is the error in the following code?
Almost everything I endure now resolves the error. It also disappears when I turn off optimization (or even when I optimize, but turn off inlining). My only suspicion is that it might be related to a vector of vectors (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);
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 (here is the main function)
#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 .