|
Segmentation Fault? Researched... Couldn't Fix It. |
Durnus
Member #7,997
November 2006
|
Please ignore this thread. My problem is solved. (I re-wrote the code.) After spending a long time on my code, I got a segmentation error. My ability isn't the best... but I want this to work. My fault occurs not while compiling, but when I run the program. There is no problem with syntax or anything, my program just crashes. If anyone could explain how to make it work, that would be great. Here is my code. The class "particle_array" and all its functions are in the header file and another file. The main file:
Spacing may be wierd, but it shouldn't be all that hard. Thanks in advance for looking at my code.
|
kazzmir
Member #1,786
December 2001
|
Im pretty lazy right now so I'm not gonna look through the whole thing, but I would suggest doing buffer = create_bitmap(800,600); after the call to set_gfx_mode. |
Durnus
Member #7,997
November 2006
|
Thanks for the quick response, I edited my code and recompiled, but it didn't fix the problem. I've read somewhere that the debug will tell you the position of the bug, but I'm using Dev-Cpp and it won't tell me.
|
Kitty Cat
Member #2,815
October 2002
|
And I wouldn't use show_mouse on anything other than the screen. Use As for the crash.. check all return values. If a function can fail, check to make sure it doesn't. -- |
Durnus
Member #7,997
November 2006
|
Sorry... I'm not that good at debugging... Checking return values? How do you do that, and what exactly does it accomplish? I just found that when I make a break point in the code, it doesn't even get into the main part of the code. (The main function. )
|
Kitty Cat
Member #2,815
October 2002
|
Quote: Checking return values? How do you do that, and what exactly does it accomplish? When the function returns, it'll give you back a value, assuming it doesn't return void. Like create_bitmap returns a BITMAP*. If that fails, the return value will be NULL. So you'd check: buffer = create_bitmap(800,600); if(buffer == NULL) { allegro_message("Couldn't create bitmap!"); exit(1); } If buffer isn't NULL, then it's a valid bitmap. Check Allegro's manual for what the other functions return for failure. -- |
Three Harris
Member #6,226
September 2005
|
could use a switch |
Kris Asick
Member #1,424
July 2001
|
I don't understand why you are using a three-dimensional array for your particles, but a segmentation fault could be indicative of illegal array access. Try commenting out the particle system and see if it runs, if so, then that's where your problem is and it probably has to do with going outside the array bounds. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Durnus
Member #7,997
November 2006
|
@Kitty Cat: @Three Harris: @Kris Asick: Nothing I'm trying is working... maybe something is wrong with my header file? Header: 1#ifndef TWOSSTUFF
2#define TWOSSTUFF
3
4class particle_array
5{
6 public:
7
8void draw_particles(BITMAP *buffer);
9void update_particle(int j, int i, bool alternate);
10
11void refreshmoved();
12
13void update_particles();
14
15int particles[400][400][3];
16int particleselected[400][400];
17};
18#endif
Other file: 1#include <allegro.h>
2#include "TWOSheader.h"
3#include <ctime>
4#include <cstdlib>
5
6
7
8
9
10void particle_array::draw_particles(BITMAP *buffer)
11{
12 for(int i = 0; i < 300; i++)
13 {
14 for(int j = 0; j < 300; j++)
15 {
16 if(particles[j]<i>[0]==1)
17 {
18 rectfill( buffer, j*2, i*2, j*2+1, i*2+1, makecol( 100, 100, 100));
19 }
20 if(particles[j]<i>[0]==2)
21 {
22 rectfill( buffer, j*2, i*2, j*2+1, i*2+1, makecol( 221, 190, 66));
23 }
24 if(particles[j]<i>[0]==3)
25 {
26 rectfill( buffer, j*2, i*2, j*2+1, i*2+1, makecol( 0, 0, 240));
27 }
28 }
29 }
30}
31void particle_array::refreshmoved()
32{
33 for(int i = 300; i > 0; i--)
34 {
35
36 for(int j = 0; j < 300; j++)
37 {
38 particles[j]<i>[1] = 0;
39 particleselected[j]<i> = 0;
40
41 }
42 }
43}
44
45
46
47void particle_array::update_particles()
48{
49 bool goagain = true;
50 bool alternate = true;
51
52 while(goagain == true);
53 {
54 alternate = !alternate;
55 goagain = false;
56
57 for(int j = 0; j < 300; j++)
58 {
59 for(int i = 300; i > 0; i--)
60 {
61 if(particleselected[j]<i> != 1)
62 {
63 srand(time(0));
64 if(rand()%7 == 1)
65 {
66 particle_array::update_particle(j,i, alternate);
67 particleselected[j]<i> = 1;
68 }
69 else
70 {
71 goagain = true;
72 }
73 }
74 }
75 }
76
77
78 }
79
80}
81
82void particle_array::update_particle(int j, int i, bool alternate)
83{
84 if(particles[j]<i>[1] != 1) // Has it moved?
85 {
86 alternate = !alternate;
87
88
89
90 if(particles[j]<i>[0]==2) // If the particle is Sand
91 {
92
93
94 if(particles[j][i+1][0] == 0)
95 {
96 particles[j][i+1][0] = 2;
97 particles[j]<i>[0] = 0;
98 particles[j][i+1][1] = 1;
99 }
100
101 else if(particles[j-1][i+1][0] == 0 && alternate == true)
102 {
103 if(j > 0)
104 {
105 particles[j-1][i+1][0] = 2;
106 particles[j]<i>[0] = 0;
107 particles[j-1][i+1][1] = 1;
108 }
109 }
110 else if(particles[j+1][i+1][0] == 0 && alternate == false)
111 {
112 if(j < 400)
113 {
114 particles[j+1][i+1][0] = 2;
115 particles[j]<i>[0] = 0;
116 particles[j+1][i+1][1] = 1;
117 }
118 }
119
120
121
122 else if(particles[j][i+1][0] == 3)
123 {
124 particles[j][i+1][0] = 2;
125 particles[j]<i>[0] = 3;
126 particles[j][i+1][1] = 1;
127 }
128
129
130 else if(particles[j+1][i+1][0] == 3 && alternate == false)
131 {
132 particles[j+1][i+1][0] = 2;
133 particles[j]<i>[0] = 3;
134 particles[j+1][i+1][1] = 1;
135 }
136 else if(particles[j-1][i+1][0] == 3 && alternate == false)
137 {
138 particles[j-1][i+1][0] = 2;
139 particles[j]<i>[0] = 3;
140 particles[j-1][i+1][1] = 1;
141 }
142
143
144 }
145 else if(particles[j]<i>[0]==3) // If the particle is Water
146 {
147
148
149 if(particles[j][i+1][0] == 0)
150 {
151 if(i < 400)
152{
153 particles[j][i+1][0] = 3;
154 particles[j]<i>[0] = 0;
155 particles[j][i+1][1] = 1;
156}
157 }
158 else if(particles[j+1][i+1][0] == 0)
159 {
160 if(j < 400 && alternate == false)
161{
162 particles[j+1][i+1][0] = 3;
163 particles[j]<i>[0] = 0;
164 particles[j+1][i+1][1] = 1;
165}
166
167 }
168 else if(particles[j-1][i+1][0] == 0)
169 {
170 if(j > 0)
171{
172 particles[j-1][i+1][0] = 3;
173 particles[j]<i>[0] = 0;
174 particles[j-1][i+1][1] = 1;
175}
176 }
177
178 else if(particles[j+1]<i>[0] == 0 && particles[j-1]<i>[0] == 0)
179 {
180
181 if(alternate)
182 {
183
184 if(j < 300)
185 {
186
187 particles[j+1]<i>[0] = 3;
188 particles[j]<i>[0] = 0;
189 particles[j+1]<i>[1] = 1;
190 }
191
192 }
193 if(!alternate)
194 {
195
196 if(j > 0)
197 {
198
199 particles[j-1]<i>[0] = 3;
200 particles[j]<i>[0] = 0;
201 particles[j-1]<i>[1] = 1;
202
203 }
204 }
205 }
206 else if(particles[j+1]<i>[0] == 0)
207 {
208
209 if(j < 300)
210{
211
212 particles[j+1]<i>[0] = 3;
213 particles[j]<i>[0] = 0;
214 particles[j+1]<i>[1] = 1;
215}
216
217 }
218 else if(particles[j-1]<i>[0] == 0)
219 {
220
221 if(j > 0)
222{
223
224 particles[j-1]<i>[0] = 3;
225 particles[j]<i>[0] = 0;
226 particles[j-1]<i>[1] = 1;
227
228}
229 }
230
231
232 }
233 }
234}
Yes, I know the spacing is all wrong. (Edit: )
|
CGamesPlay
Member #2,559
July 2002
|
As a tip, remove the paramter from display_elements; you don't use it. Comment out the particle_array part line, for me... That line causes your program to allocate 2.5 megabytes on the stack, which is probably the crash. -- Ryan Patterson - <http://cgamesplay.com/> |
Durnus
Member #7,997
November 2006
|
I just commented out all the particle stuff, but the error still happens... I just commented out the header file, but the error still happens. Trying to think of when the error started happening... It happened when I split the class into multiple files. That help? (Edit)
|
CGamesPlay
Member #2,559
July 2002
|
Quote: while(goagain == true); Take off the semicolon, of course. Do this exactly 1 time at the beginning of your program, not several thousand times each second... it's just giving you the same random number for that whole second. I ran your code, and it hung at that while loop. Then it hung someplace else. I commented out the particle code from the main function, and it crashed in the rectfill call. No, I don't know why. But I actually got a window created. If you are still having the crash before set_gfx_mode, then you need to make sure you are actually rebuilding the code. -- Ryan Patterson - <http://cgamesplay.com/> |
Durnus
Member #7,997
November 2006
|
I'm rebuilding it every time. It does not crash at the set_gfx_mode, it crashes before the first thing I put in my main function. ...since it got so much further for you, I think I should reboot my computer or something. Rebooted, still not working. I think I'm just going to re-write it. (EDIT) Here is the code:
|
Kitty Cat
Member #2,815
October 2002
|
You're trying to use an Allegro function before Allegro is initialized. -- |
Durnus
Member #7,997
November 2006
|
Oh jeez... I feel so stupid. Okay, thanks.
|
Kris Asick
Member #1,424
July 2001
|
One final tip. When something doesn't work, comment EVERYTHING in your main loop, then run the program. It should run fine since, well, nothing's happening! If it doesn't, your global variable initialization code is doing things it shouldn't. Then, start uncommenting sections one by one, compiling each time to see if it still runs or not. When it finally fails, then you have a better idea of what's causing an error like that. Oh, and you don't need the semicolon on END_OF_MAIN(). --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Neil Walker
Member #210
April 2000
|
And use an IDE with an integrated debugger Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
HoHo
Member #4,534
April 2004
|
... or lots of printf()'s! __________ |
Durnus
Member #7,997
November 2006
|
I am using Dev-C++. A segmentation error IS NOT A COMPILE ERROR. It is fixed now, please leave this thread to die.
|
BAF
Member #2,981
December 2002
|
The only thing I saw related to compile error is the ; after END_OF_MAIN. The other tips about commenting stuff out were debugging tips. |
|