Aquarius_Girl
Aquarius_Girl

Reputation: 22906

Logical error: Unable to determine the cause of segmentation fault

    for (i = (routeVector.size () - 1); i >= 0; i--)
    {
        cout << "Connected to -> " << routeVector[i].exitPoint;
        for (j = (routeVector.size () - 1); j >= 0; j--)
        {
            if (routeVector[i].selectedBranchesVector.size() > 0)
            {
                cout << "\n: routeVector[i].selectedBranchesVector[0].connectedExitPoint" <<
routeVector[i].selectedBranchesVector[0].connectedExitPoint;

                ******cout << "\nrouteVector[j].exitPoint:" << routeVector[j].exitPoint;
                if (routeVector[i].selectedBranchesVector[0].connectedExitPoint == routeVector[j].exitPoint)
                {
                    cout << "Connected to -> " << routeVector[i].selectedBranchesVector[0].connectedExitPoint;
                }
            }
        }
    }

The stared line is giving me a segmentation fault, I fail to understand why.

If the "routeVector" had nothing in "selectedBranchesVector", it wouldn't have even reached the inside if.

What can be the cause of the said problem?

EDIT 1:

To make the problem more clear, I printed out the two conditions of the statement and the error is shown on the stared line.

The structures are:

typedef struct branch
{
    unsigned int distance;
    int          connectedExitPoint;
} branch;

typedef struct route
{
    int    exitPoint;
    vector <branch> selectedBranchesVector;
} route;

vector <route> routeVector;

Upvotes: 0

Views: 107

Answers (1)

Nim
Nim

Reputation: 33655

This is dependent on the type of i and j - if i and j are unsigned, the above loops will loop back around quite happily - which is probably what is going on - print the indexes and you'll see...

Upvotes: 1

Related Questions