Segmentation Fault

I'm currently trying to code a BFS styled program for an assignment of mine. Logically speaking, I think I have it down but it was taking too long to execute so I removed a for loop, and instead used stf::find from #include <algorithm>. Since then my program keeps crashing at runtime and while debugging I get a Segmentation fault.

The user will input a string of 25 characters that are digits(1-4) with the inclusion of one character("G").

Any and all help will be appreciated. I also apologize in advance for posting my entire code, but I couldn't figure out where the error might be from.


Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
#include<algorithm>

using namespace std;

char directions[4] = {'L', 'R', 'U', 'D'};
//string direcPrint[4] = {"Left", "Right", "Up", "Down"} ;
const int pondSize = 5;

struct frontier{
	int currentPad;
	string currentPath;
	vector <int> previousPads;
	
};

struct solution{
	string pathway;
	int nodesLooked;
	int length;
};

int nextPad(int cPad, int dist, int direc)
{
	//cPad is the index of the current lilypad
	//dist is distance jumped(1-4)
	//direc is the direction(L,R,U,D)
	
	int nPad = -1; //-1 by default
	int currentCol;
	
	if (direc == 0)		//Left
	{
		currentCol = cPad % pondSize;
		if (currentCol >= dist)
		{
			nPad = cPad-dist;		
		}	
	}
	else if (direc == 1)	//Right
	{
		currentCol = cPad % pondSize;
		if (currentCol + dist < pondSize)
		{
			nPad = cPad + dist;
		}
	}
	else if (direc == 2)	//Up
	{
		nPad = max(max(-1, cPad), (dist*pondSize));
	}
	else if (direc == 3)	//Down
	{
		nPad = cPad + (dist*pondSize);
		if (nPad >= (pondSize^2))
		{
			nPad = -1;
		}
	}
	else
		nPad = -1;
	
	return nPad;
}

solution shortestPath(string pondPath)
{
	int pad;
	string path;
	vector <int> pads;	//Same as previousPads
	vector <int>::iterator it;
	solution sol;
	queue <frontier> fringe;
	frontier currentItem;
	currentItem = {0,"",vector<int>()};
	
	currentItem.currentPad = 0;
	currentItem.currentPath = "";
	currentItem.previousPads.push_back(0);
	
	
	fringe.push(currentItem);
	
	int nodeLooked = 0;
	int i = 0;
	int counting = 0;
	while (!fringe.empty())
	{
		
		frontier currentFringe;
		currentFringe = fringe.front();
		pad = currentFringe.currentPad;
		path = currentFringe.currentPath;
		pads.push_back(currentFringe.previousPads[i]);
		nodeLooked++;
		i++;
		
		if (pondPath[pad] == 'G')
		{
			break;
		}
		
	
		int dist;
		int futurePad;
		
		for(int jump=0; jump<4; jump++)
		{
			char temp = pondPath[pad];
			dist = (int)temp;
			futurePad = nextPad(pad, dist, jump);
			
			if (futurePad != -1)
			{
				it = find(pads.begin(), pads.end(), futurePad);
					if(it != pads.end())
					{
						
						break;
					}
					else
					{
						//i++;
						string futurePath = path + directions[jump];
						pads.push_back(futurePad);
						currentFringe.currentPad = futurePad;
						currentFringe.currentPath = futurePath;
						currentFringe.previousPads = pads;
						fringe.push(currentFringe);
					}
			} 
			
		}
		path = "No way to the solution!!";
	}
	
	sol.pathway = path;
	sol.nodesLooked = nodeLooked;
	sol.length = fringe.size();
	
	return sol;
}


int main()
{
	solution answer;
	string userInput = "";
	clock_t t1, t2;
	cout<<"Please enter your string: ";
	getline(cin, userInput);
	cout<<"This is the pond that you have entered: "<<endl;
	cout<<userInput<<endl;
	t1 = clock();
	cout<<clock();
	answer = shortestPath(userInput);
	t2 = clock();
	cout<<endl<<t2<<endl;
	cout<<endl;
	cout<<"The solution is: "<<answer.pathway<<endl;
	cout<<"Nodes looked at: "<<answer.nodesLooked<<endl;
	cout<<"Nodes not looked at: "<<answer.length<<endl;
	float diff ((float)t2-(float)t1);
    cout<<"time is: "<<diff<<endl;
    
    system("PAUSE");
    return 0;
	
}
Last edited on

So a user enters a string,
Ex: "232413213111232433331411G"

Which then co-relates to something like this:
2 3 2 4 1
3 2 1 3 1
1 1 2 3 2
4 3 3 3 3
1 4 1 1 G

Essentially, you can jump left(L), right(R), up(U) or down(D) the number of spaces depending on the number at that index.

The end result will be the shortest path to G. So for the given example the solution would be: RDRD
Last edited on
The crash happens at pads.push_back (currentFringe.previousPads[i]); previousPads size is 1 and i is 1 so classical out of range error
I've changed "i" to "0". Because I need to keep looking at the first element. And I've added fringe.pop() in my if else of my for loop.

It gives me an infinite loop now.
show your updated code.
also, comment your `shortestPath()' function


run through a debugger, you may send an interrupt signal (<C-c>) and then perform a backtrace.
Topic archived. No new replies allowed.