seg fault

hi I just wrote a function about the Breadth First Traversal. when i try to compile it,it return a seg fault error, i don't know how can i fix that , can anybody give a hint..

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
void Map::breadthFirstTraversal(int t)
//int *visited;
//cityMap is the container that store the names of city from a input file
{
	for(int i = 1 ; i<= cityMap.size(); i++)
	visited[i] = 0;
	queue <int> q;
	q.push(t);
	
	while(!q.empty())
	{
		int s = q.front();
		q.pop();
		visited[s] = 1;
		cout<<s<<endl;
		
		for(int i = 1; i<=cityMap.size(); i++)
		{
			if(cityMap.size()!=0 && visited[i] == 0)
			{
				q.push(i);
				visited[i] = 1;
			}		
		}
	}
}


thanks
Any particular reason...
1. why are you skipping first element?
2. why are you past last element?
 
for(int i = 1 ; i<= cityMap.size(); i++)
sorry , i just mistype it, but since i change to int i = 0 , i<cityMap.size() i++.. it still return a seg fault.
1
2
3
for(int i = 1; i<=cityMap.size(); i++)
{
	if(cityMap.size()!=0 && visited[i] == 0)

There is no need to check section in bold since loop will not run either if size is 0.

Generally whole code is a mess and pretty dangerous to use if you ask me, please if you can, reorganize your data better.
Last edited on
You need to run your program with the debugger, which will show you the line that causes the segmentation fault.
If you use GCC, you also should define _GLIBCXX_DEBUG, which will alert you to any out-of-bounds accesses in a standard container.
Topic archived. No new replies allowed.