Can't understand why I'm getting a segmentation fault inside these for-loops

I have a container of a class type which I need to iterate over in several different functions. When the code initially runs, the container always has one object in it, but later on it may be empty.

It's a couple hundred lines of code, so here's just some code for context and the first function that ends with a segfault. That being said, I'm starting to wonder if the problem might be in a totally different location than the debugger claims.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void M::pathFind(...) {
    vector<Node> openpaths, closedpaths;
    Node newnode(this->x, this->y, this->x, this->y, 0, 0, 0, 0, 0);
    openpaths.push_back(newnode);

    //...
    while (true) {
        findF(openpaths);
        //...
    }
    //...
}

//....

void M::findF(vector<Node> &openpaths) {
    for (int i = 0, j = openpaths.size(); i != j; ++i) {
        openpaths[i].F = openpaths[i].H + openpaths[i].G; ////SEGFAULT
    }
    return;
}



I've been staring at this for a few hours, and I can't see anything wrong with the logic of the loop. Any ideas where the problem might be?
And what is your logic for line 17?

Can you do this:

for (int i = 0, i < openpaths.size(); ++i) {

Or make use of the begin & end iterators.



Are the values in H & G initialised?

HTH
Last edited on
Topic archived. No new replies allowed.