This code can not execute well in Ubuntu, but did work in Windows

I have no idea why this happened, but in terminal, it compile well, and when I execute it, it said Segmentation fault (core dumped)

And in Eclipse, it is"warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000"in debugger (Well, this fault seems not that fatal)

I will put my code below.

Also, I make a new code(actually it is an old version....)

The new one worked well, and when I debug the old one, when the call checker(aa,bb,15,17,19,21,23,25), the checker receive a wrong number!!! (aa=0 and bb=2 at that time)
Here is what happened in debugger:
Program received signal SIGBUS, Bus error.
0x0000000000400c01 in checker (a=33554432, b=0, c=0, d=17, e=19, f=21, g=23,
h=25) at Eight_Queen.cpp:35


I have no idea why this happened!!! Anyone know please tell me!!!!

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

OLD!!!

 http://ainoob.com/files/programs/c_programs/eight_queen.cpp


NEW!!!!
#include "iostream"
using namespace std;
int (*p)[8] = new int [100][8];
int ttime=0;
void show(int a,int b)
{
	a--;
	b--;
	for(;a<(b+1);a++)
	{
		for(int x=0;x<8;x++)
		{
			int xx=p[a][x];
			for(int xxx=xx;xxx>0;xxx--)
			{
				cout<<"0 ";
			}
			cout<<"X ";
			for(int xxxx=(7-xx);xxxx>0;xxxx--)
			{
				cout<<"0 ";
			}
			cout<<endl;
		}
		cout<<endl;
	}
}
int check(int a,int b,int c,int d, int e,int f,int g,int h)
{
	int xxxxx=0;
	int test1=0;
									int c1[8]={0,0,0,0,0,0,0,0};
									int c2[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
									int c3[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
									c1[a]++;
									c1[b]++;
									c1[c]++;
									c1[d]++;
									c1[e]++;
									c1[f]++;
									c1[g]++;
									c1[h]++;
									for(int loop2=0;loop2<8;loop2++)
									{
										if(c1[loop2]>1)
										{
										test1++;
										}
									}
									if (test1==0)
							        {
							        	int test2=0;
										c2[8-a]++;
										c2[9-b]++;
										c2[10-c]++;
										c2[11-d]++;
										c2[12-e]++;
										c2[13-f]++;
										c2[14-g]++;
										c2[15-h]++;
										for (int loop4=0;loop4<15;loop4++)
										{
											if(c2[loop4]>1)
											{
											test2++;
											}
									    }
										if(test2==0)
										{	
											int test3=0;
											c3[1+a]++;
											c3[2+b]++;
											c3[3+c]++;
											c3[4+d]++;
											c3[5+e]++;
											c3[6+f]++;
											c3[7+g]++;
											c3[8+h]++;
											for (int loop6=0;loop6<15;loop6++)
											{
												if(c3[loop6]>1)
												{
													test3++;
												}
											}
											if(test3==0)
											{
												p[ttime][0]=a;
												p[ttime][1]=b;
												p[ttime][2]=c;
												p[ttime][3]=d;
												p[ttime][4]=e;
												p[ttime][5]=f;
												p[ttime][6]=g;
												p[ttime][7]=h;
												ttime++;
												cout<<".";
												xxxxx=1;
											}
										}			
									}
									return xxxxx;
} 
int main()
{
	for (int aa=0;aa<8;aa++)
	{
		for (int bb=0;bb<8;bb++)
		{
			for (int cc=0;cc<8;cc++)
			{
				for (int dd=0;dd<8;dd++)
				{
					for(int ee=0;ee<8;ee++)
					{
						for (int ff=0;ff<8;ff++)
						{
							for (int gg=0;gg<8;gg++)
							{
								for (int hh=0;hh<8;hh++)
								{
									check(aa,bb,cc,dd,ee,ff,gg,hh);
								}
							}
						}
					}
				}
			}
		}
	}
	cout<<endl<<"The calculating is finished,there are "<<ttime<<" qualified examples."<<endl<<"Do you want to see them?"<<endl;
	cout<<"'1' means yes, and '0' means no"<<endl;
	int cinsee;
	cin>>cinsee;
	looper:
	if(cinsee)
	{
		cout<<"Input the starting example and ending example you want to see"<<endl<<"Start with the starting example"<<endl;
		firstcin:
		int cinstart;
		cin>>cinstart;
		if(cinstart<1||cinstart>ttime)
		{
			cout<<"Out of range, please input another starting number"<<endl;
			goto firstcin;
		}
		cout<<"Now the Ending example"<<endl;
		secondcin:
		int cinend;
		cin>>cinend;
		if(cinend<cinstart||cinend>ttime)
		{
			cout<<"Out of range, please input another starting number"<<endl;
			goto secondcin;
		}
		show(cinstart,cinend);
	}
	cout<<"To show another example, please input 1. To quit, please input 0"<<endl;
	cin>>cinsee; 
	if(cinsee)
	goto looper;
}
Last edited on
By the error message, I suppose you're using GCC.
I don't use Linux, so I don't know what this is all about, but can you identify the line or at least the method inside which the error pops up?
Well, I can not know which line is error, because it can be compiled, but when it can not be execute well.

If I know which line is wrong, I think I can modify it
The most important thing is that I do not know how to debug...

I learn a little assembly in Windows.
Since you are running linux, you don't need to learn assembly in order to debug.

- Open your terminal - ctrl + alt + t
- type: sudo (choose one of these)[apt-get, aptitude] install gdb
ex: sudo aptitude install gdb OR sudo aptitude install gdb
- Type password if it is required

-When that has finished, compile your program on the command line and add -g option to produce debug information

-type into terminal:
gdb program_name

-type `run` and provide any input the program needs until you get to the segfault, this should show you the line that produced it.

Paste that output here
Last edited on
Thanks, I am now using gdb to debug this program, I think I find something that show the problem, I will try to find the problem!
Topic archived. No new replies allowed.