stack overflow.... help...

how to solve the"stack overflow"problem?????

i'm practicing to write a program about "maze"
the following are parts of my code
it have a problem of stack overflow...
i really don't know how to solve it
please help me

ps. it is the gogo function which get the stack overflow problem

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
  
void gogo(vector<vector<int> > &maze, int p, int q){
	int ways=0, go;
	
	
	srand(time(NULL));	
	go=rand()%4;
	
	maze[p][q]=3;
	
	
	if(maze[p-1][q]==0||maze[p-1][q]>=201){
		ways++;
	}	
	if(maze[p+1][q]==0||maze[p+1][q]>=201){
		ways++;
	} 	
	if(maze[p][q-1]==0||maze[p][q-1]>=201){
		ways++;
	}	
	if(maze[p][q+1]==0||maze[p][q+1]>=201){   
		ways++;
	} 
	
	
	

	
	if(ways>=2){ 
		r=p;
		k=q;
	}
	else if(ways<=1){
		r=r;
		k=k;
	}
						            

	//system("pause");		
	
	//////////////////////////////////
	if(go==0&&(maze[p-1][q]==0||maze[p-1][q]>=201)){ 
		maze[p-1][q]==3;	  	  
		printrealmaze(maze);
		gogo(maze,p-1,q);    
	}	
	if(go==1&&(maze[p+1][q]==0||maze[p+1][q]>=201)){  
		maze[p+1][q]=3; 		       
		printrealmaze(maze);
		gogo(maze,p+1,q);    
	}	
	if(go==2&&(maze[p][q+1]==0||maze[p][q+1]>=201)){ 
		maze[p][q+1]=3;  		      
		printrealmaze(maze);
		gogo(maze,p,q+1);    			
	}	
	if(go==3&&(maze[p][q-1]==0||maze[p][q-1]>=201)){ 
		maze[p][q-1]=3;		      
		printrealmaze(maze);
		gogo(maze,p,q-1);    
	}
	////////////////////////////////
	if(go==0&&maze[p-1][q]!=0&&maze[p-1][q]<200&&ways!=0){
		gogo(maze,p,q);
	}	
	else if(go==1&&maze[p+1][q]!=0&&maze[p+1][q]<200&&ways!=0){
		gogo(maze,p,q);
	}	
	else if(go==2&&maze[p][q+1]!=0&&maze[p][q+1]<200&&ways!=0){
		gogo(maze,p,q);			
	}
	else if(go==3&&maze[p][q-1]!=0&&maze[p][q-1]<200&&ways!=0){
		gogo(maze,p,q);
	}

	
	///////////////////////////////////
	else if(ways==0&&(p!=-3+maze[0].size()&&q!=-3+maze.size())){//
	    p=r;
	    q=k;
	    
		gogo(maze,p,q);
	}
	cout<<"GG"<<endl;
	
	exit(1);

}
Last edited on
You are recursevly call your function. Looks like it is either inpt vector is too large or you have infinite recursion loop (more likely)
how to solve it
Either try to get rid of recursion (or at least make it into tail recursion), or step into your function with debugger and try to see if it gets into infinite recursion
Topic archived. No new replies allowed.