segmentation fault

Hey guys,
I'm trying to make a Game of life. But every time I count neighbours I get segmentation fault, what's the reason behind that?
This is the code that i have written so far.
The problem is in method "calculate", but i'm not able to figure it out.
Thx in advance.


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

/*
 * GameOfLife.cpp
 *
 *  Created on: Dec 7, 2011
 *      Author: unstoppy
 */

#include <iostream>
#include <vector>

#include "GameOfLife.h"

using namespace std;

GameOfLife::GameOfLife()  {
	int x = 0;
	int y = 0;

	vector < vector <bool> > v1(0, vector<bool>(0));

	for (int i = 0; i < v1.size(); i++){
		for (int j = 0; j < v1.size(); j++){
			v1[i][j] = false;
			}
		}

//2nd vector

//the start position of glider
	vector < vector <bool> > vect(0, vector<bool>(0));
	for (int i = 0; i < vect.size(); i++){
		for (int j = 0; j < vect.size(); j++){
			vect[i][j] = false;
			}
		}	
	}

GameOfLife::~GameOfLife() {

}

void GameOfLife::print1(vector<vector <bool> > &v){

	for (int i = 0; i < 10; i++){
		for (int j = 0; j < 40; j++){
			if (v[i][j] == true){				
				cout <<" *";
			}
			else{
				cout <<"  ";
			}

			}
		cout <<endl;
		}
	
}



void GameOfLife::calculate(vector<vector <bool> > &v){
	vector < vector <bool> > v1(40, vector<bool>(10));	
	int neighbour = 0;
	for (int m=0; m < v.size(); m++){
		for (int n=0; m < v.size(); n++){
			if (v[m-1][n-1] == true)
					neighbour += 1;
			if (v[m-1][n] == true)
					neighbour += 1;
			if (v[m-1][n+1] == true)
					neighbour  += 1;
			if (v[m][n-1] == true)
					neighbour  += 1;
			if (v[m][n+1] == true)
					neighbour  += 1;
			if (v[m+1][n-1] == true)
					neighbour  += 1;
			if (v[m+1][n] == true)
					neighbour  += 1;
			if (v[m][n+1] == true)
					neighbour  += 1;

			//Applying the rules
			if (v[m][n]==true && (neighbour == 2 || neighbour == 3))
				v1[m][n] = true;

			if (v[m][n]==true && (neighbour > 4 || neighbour < 2))
				v1[m][n] = false;

			if (v[m][n]==false && neighbour == 3)
				v1[m][n] = true;
			}

		}
                 v.swap(v1);
	}

//---------------------------------------------------------------------------------

int main()
{
	GameOfLife glider;
	
	//glider.print1();
	vector < vector <bool> > vec(40, vector<bool>(10));	
	vec[5][1] = true;
	vec[5][2] = true;
	vec[6][1] = true;
	vec[6][2] = true;

	vec[3][13] = true;
	vec[3][14] = true;
	vec[4][12] = true;
	vec[5][11] = true;
	vec[6][11] = true;
	vec[7][11] = true;
	vec[8][12] = true;
	vec[9][13] = true;
	vec[9][14] = true;

	vec[6][15] = true;
	vec[4][16] = true;
	vec[5][17] = true;
	vec[7][17] = true;
	vec[6][17] = true;
	vec[6][18] = true;
	vec[8][16] = true;

	vec[3][21] = true;
	vec[4][21] = true;
	vec[5][21] = true;

	vec[2][23] = true;
	vec[1][25] = true;
	vec[2][25] = true;

	vec[6][23] = true;

	vec[3][35] = true;
	vec[3][36] = true;
	vec[4][35] = true;
	vec[4][36] = true;

	vec[3][22] = true;
	vec[4][22] = true;
	vec[5][22] = true;
	vec[6][25] = true;
	vec[7][25] = true;
	
	

	glider.print1(vec);
	glider.calculate(vec);
	glider.print1(vec);
}


Last edited on
You are accessing out of bounds in the calculate function. (-1 and size)
Either check the borders separately, or make centinels there
thx for ur reply. i'm new to c++. i don't know how to make centinels. can u help me plz?
The idea is simple. Put in the border cells some value that will not affect the calculations.
In this case you could use false

So you create your matrix a little bigger, and you say that the world goes from 1 to size()-1
1
2
3
//calculate method
	for (int K=1; K+1 < v.size(); ++K){
		for (int L=1; L+1 < v[K].size(); ++L){

By the way, you've got a mistake in line 66. You are checking the condition on the wrong index.
Last edited on

once again thx!

u meant this by line 66?
if (v[m-1][n-1] == true)

i think it has to be checked coz u need to look for all the neighbours which surround the cell.
so if the cell i'm checking is "a11"

[ [a00, a01, a02]
[a10, a11, a12]
[a20, a21, a22]]

I think i have to check all the cells a00 too. am i right?
line 66: for (int n=0; m < v.size(); n++){ Note that you are looping with n but the condition is against m

Yep, the neighbourhood is correct.

By the way, I found obfuscated to write v[m][n] == false. You already have a boolean, so check it directly
1
2
not v[m][n] //replacing ==false
v[m][n] //replacing ==true 
Last edited on

thx. line 66 was a stupid mistake by me.
i didnt know before that "not" can be used in place of false statement.
i have edited these codes. but i have tried printing it but i dont think i'm getting the right result yet.
do u have any suggestion how can i display it like an animation? i think i have to use an infinite loop to change from one state to another.
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

/*
 * GameOfLife.cpp
 *
 *  Created on: Dec 7, 2011
 *      Author: unstoppy
 */

#include <iostream>
#include <vector>

#include "GameOfLife.h"

using namespace std;

GameOfLife::GameOfLife()  {
	int x = 0;
	int y = 0;

	vector < vector <bool> > v11(0, vector<bool>(0));

	for (int i = 0; i < v11.size(); i++){
		for (int j = 0; j < v11.size(); j++){
			v11[i][j] = false;
			}
		}

//2nd vector

//the start position of glider
	vector < vector <bool> > vect(0, vector<bool>(0));
	for (int i = 0; i < vect.size(); i++){
		for (int j = 0; j < vect.size(); j++){
			vect[i][j] = false;
			}
		}	
	}

GameOfLife::~GameOfLife() {

}

void GameOfLife::print1(vector<vector <bool> > &v){

	for (int i = 0; i < v.size(); i++){
		for (int j = 0; j < v.size(); j++){
			if (v[i][j] == true){				
				cout <<" *";
			}
			else{
				cout <<"  ";
			}

			}
		cout <<endl;
		}
	
}



void GameOfLife::calculate(vector<vector <bool> > &v){
	vector < vector <bool> > v1(40, vector<bool>(10));	
	int neighbour = 0;
	for (int m=1; m < (v.size()-1); m++){
		for (int n=1; n < (v.size()-1); n++){
			if (v[m-1][n-1] == true)
					neighbour += 1;
			if (v[m-1][n] == true)
					neighbour += 1;
			if (v[m-1][n+1] == true)
					neighbour  += 1;
			if (v[m][n-1] == true)
					neighbour  += 1;
			if (v[m][n+1] == true)
					neighbour  += 1;
			if (v[m+1][n-1] == true)
					neighbour  += 1;
			if (v[m+1][n] == true)
					neighbour  += 1;
			if (v[m][n+1] == true)
					neighbour  += 1;
			
		
			//Applying the rules
			if (v[m][n] && (neighbour == 2 || neighbour == 3))
				v1[m][n] = true;

			if (v[m][n] && (neighbour > 4 || neighbour < 2))
				v1[m][n] = false;

			if (not v[m][n] && neighbour == 3)
				v1[m][n] = true;
			}

		}v.swap(v1);
	}

//---------------------------------------------------------------------------------

int main()
{
	GameOfLife glider;
	
	//glider.print1();
	vector < vector <bool> > vec(40, vector<bool>(10));	
	vec[5][1] = true;
	vec[5][2] = true;
	vec[6][1] = true;
	vec[6][2] = true;

	vec[3][13] = true;
	vec[3][14] = true;
	vec[4][12] = true;
	vec[5][11] = true;
	vec[6][11] = true;
	vec[7][11] = true;
	vec[8][12] = true;
	vec[9][13] = true;
	vec[9][14] = true;

	vec[6][15] = true;
	vec[4][16] = true;
	vec[5][17] = true;
	vec[7][17] = true;
	vec[6][17] = true;
	vec[6][18] = true;
	vec[8][16] = true;

	vec[3][21] = true;
	vec[4][21] = true;
	vec[5][21] = true;

	vec[2][23] = true;
	vec[1][25] = true;
	vec[2][25] = true;

	vec[6][23] = true;

	vec[3][35] = true;
	vec[3][36] = true;
	vec[4][35] = true;
	vec[4][36] = true;

	vec[3][22] = true;
	vec[4][22] = true;
	vec[5][22] = true;
	vec[6][25] = true;
	vec[7][25] = true;

	
	glider.print1(vec);
	glider.calculate(vec);
	glider.calculate(vec);
	glider.calculate(vec);
	
	
	glider.print1(vec);
	
}



Last edited on
Sorry, I explained it badly.
1
2
3
			if (not v[m][n] && neighbour == 3)
				v1[m][n]; //statement has no effect
			}
You need to set the value there v1[m][n] = true;

I was talking about wheter you check the value.
Remember that comparison == and assignment = are different. The idea was to replace the comparisons.
I understand now. I've edited it. do u have any idea how can i display the iteration. Like this picture: http://upload.wikimedia.org/wikipedia/commons/e/e5/Gospers_glider_gun.gif
Topic archived. No new replies allowed.