Reputation: 582
I got a segmentation fault in this code and I cannot get why:
vector <double> *point;
for (int i = 0; i < point->size(); i += 3) {
for (int j = i + 3; j < point->size(); j += 3) {
if (distance((*point)[i], (*point)[i + 1],(*point)[i + 2], (*point)[j],(*point)[j + 1], (*point)[j + 2]) < treshold){
point->erase(point->begin() + j, point->begin() + j * 3);
j -= 3;
}
}
}
point is a vector of coordinates of points, something like (x1,y1,z1,x2,y2,z3,...,xn,yn,zn). Distance it's a function that calculate the euclidean distance between 2 points given the 6 coordinate. Basically what I would do is something like "if two points are too close to each other delete one of them". But I get segmentation fault. Any idea?
Upvotes: 1
Views: 1165
Reputation: 667
The erase
line is wrong. You have j * 3
when you probably mean j + 3
.
Upvotes: 4
Reputation: 258548
vector <double> *point;
declares an uninitialized pointer which you attempt to access with point->size()
.
Thus, undefined behavior & a crash.
I'd go with a simple object instead
vector <double> point;
or, if you must use dynamic memory:
vector <double> *point = new vector<double>;
Upvotes: 4