Search code examples
c++vectorsegmentation-faulterase

Vector:: erase segmentation fault


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?


Solution

  • The erase line is wrong. You have j * 3 when you probably mean j + 3.