Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Segmentation Fault? Researched... Couldn't Fix It.

Credits go to Kitty Cat for helping out!
This thread is locked; no one can reply to it. rss feed Print
Segmentation Fault? Researched... Couldn't Fix It.
Durnus
Member #7,997
November 2006
avatar

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:

1#include <allegro.h>
2#include <math.h>
3#include <cstdlib>
4#include <stdio.h>
5#include <iostream>
6#include "TWOSheader.h"
7 
8 
9using namespace std;
10 
11 
12 
13bool alternate; //used for moving sand + water back and forth
14 
15int cursorsize = 3; //size of cursor
16 
17 
18 
19int falses; //DEGUGGING
20int trues;//DEBUGGING
21 
22const bool RIGHT = true;
23const int LEFT = false;
24 
25 
26 
27BITMAP *buffer; //The buffer.
28 
29int currentelement = 1; // Selected Element
30 
31void display_elements (int current)
32{
33 rectfill(buffer,600,0,800,600,makecol(50,50,50));
34 if(currentelement == 1)
35 {
36 textout_ex( buffer, font, "Wall", 605, 10, makecol( 0, 0, 0), makecol( 255, 255, 255) );
37 textout_ex( buffer, font, "Sand", 605, 20, makecol( 200, 200, 200), makecol( 0,0,0) );
38 textout_ex( buffer, font, "Water", 605, 30, makecol( 200, 200, 200), makecol( 0,0,0) );
39
40 
41 
42 }
43 if(currentelement == 2)
44 {
45 textout_ex( buffer, font, "Wall", 605, 10, makecol( 200, 200, 200), makecol( 0, 0, 0) );
46 textout_ex( buffer, font, "Sand", 605, 20, makecol( 0, 0, 0), makecol( 255, 255, 255) );
47 textout_ex( buffer, font, "Water", 605, 30, makecol( 200, 200, 200), makecol( 0,0,0) );
48
49 
50 }
51 if(currentelement == 3)
52 {
53 textout_ex( buffer, font, "Wall", 605, 10, makecol( 200, 200, 200), makecol( 0, 0, 0) );
54 textout_ex( buffer, font, "Sand", 605, 20, makecol( 200, 200, 200), makecol( 0,0,0) );
55 textout_ex( buffer, font, "Water", 605, 30, makecol( 0, 0, 0), makecol( 255,255,255) );
56
57 
58 }
59}
60 
61 
62void interface_mouse(int xm, int ym)
63{
64 if(ym > 10 && ym < 20)
65 {
66 currentelement = 1;
67 }
68 else if(ym > 20 && ym < 30)
69 {
70 currentelement = 2;
71 }
72 else if(ym > 30 && ym < 40)
73 {
74 currentelement = 3;
75 }
76}
77 
78 
79 
80 
81int main(){
82
83 allegro_init();
84 install_keyboard();
85 install_mouse();
86
87 currentelement = 0;
88
89 particle_array part; //A new particle array object.
90
91 alternate = true;
92 
93 buffer = create_bitmap(800,600);
94
95 set_color_depth(8);
96 set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
97
98 set_window_title("Sandman's Dream");
99
100 display_elements(1);
101
102
103
104 show_mouse(buffer);
105
106 while ( !key[KEY_ESC] )
107 {
108
109 clear_keybuf();
110
111 rectfill(buffer,0,0,600,600,makecol(0,0,0));
112
113
114
115 if(mouse_b & 1 && mouse_x < 600)
116 {
117
118 for(int i = 0; i < cursorsize; i++)
119 {
120 for(int j = 0; j < cursorsize; j++)
121 {
122 if((mouse_x/2)-j >= 0)
123 {
124 part.particles[(int)floor(mouse_x/2)-j][(int)floor(mouse_y/2)-i][0] = currentelement;
125 }
126 }
127
128 }
129 }
130 if(mouse_b & 1 && mouse_x > 600)
131 {
132 interface_mouse(mouse_x,mouse_y);
133 }
134
135 if(mouse_b & 2 && mouse_x <600)
136 {
137 for(int i = 0; i < 10; i++)
138 {
139 for(int j = 0; j < 10; j++)
140 {
141 part.particles[(int)floor(mouse_x/2)-i+5][(int)floor(mouse_y/2)-j+5][0] = 0;
142 }
143 }
144
145 }
146 if(key[KEY_1])
147 {
148 cursorsize = 3;
149 }
150 else if(key[KEY_2])
151 {
152 cursorsize = 6;
153 }
154 else if(key[KEY_3])
155 {
156 cursorsize = 10;
157 }
158 for(int i = 0; i < 300; i++)
159 {
160 for(int j = 0; j < 300; j++)
161 {
162 if(part.particles[j]<i>[0]==1)
163 {
164 rectfill( buffer, j*2, i*2, j*2+1, i*2+1, makecol( 100, 100, 100));
165 }
166 }
167 }alternate = !alternate;
168
169 part.update_particles();
170 part.draw_particles(buffer);
171 part.refreshmoved();
172
173 if(mouse_x > 600)
174 {
175 display_elements(currentelement);
176 }
177
178
179
180 draw_sprite(screen, buffer, 0, 0);
181
182
183
184 rest(10);
185
186
187 
188 }
189
190 return 0;
191
192}
193END_OF_MAIN();

Spacing may be wierd, but it shouldn't be all that hard. :P

Thanks in advance for looking at my code.

kazzmir
Member #1,786
December 2001
avatar

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
avatar

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. :P

Kitty Cat
Member #2,815
October 2002
avatar

And I wouldn't use show_mouse on anything other than the screen. Use
draw_sprite(buffer, mouse_sprite, mouse_x, mouse_y);
Right before blitting the buffer to the screen. And use blit to copy the buffer to the screen, not draw_sprite (unless you want the buffer to be masked, which you usually don't.

As for the crash.. check all return values. If a function can fail, check to make sure it doesn't.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Durnus
Member #7,997
November 2006
avatar

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. :P)

???

Kitty Cat
Member #2,815
October 2002
avatar

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.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

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)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Durnus
Member #7,997
November 2006
avatar

@Kitty Cat:
Okay, I tried that, but the program doesn't get far enough along to execute the code. :P

@Three Harris:
I assume you mean I could use a switch in the interface display function. I'll do that, I guess it is more efficient.

@Kris Asick:
I tried disabling all the particle array stuff, and it still didn't work.

Nothing I'm trying is working... maybe something is wrong with my header file?

Header:

#SelectExpand
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:

#SelectExpand
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.
I hope there is something wrong with my header file. :P

(Edit: )
Now I'm thinking of just re-writing this code... it's so disorganized and horrible looking. :P And maybe I'd get rid of that... interesting 3-d array. (And make it a 4 dimensional array! Mehehehe! Just kidding.)

CGamesPlay
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Durnus
Member #7,997
November 2006
avatar

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? :P

(Edit)
Oh, thanks for the tip. Didn't realise I wasn't using it.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

while(goagain == true);

Take off the semicolon, of course.

Quote:

srand(time(0));

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Durnus
Member #7,997
November 2006
avatar

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. :P

(EDIT)
I started re-writing my code, but immediately ran into the error again.
I have cut down the cause to this: when I make a bitmap and set it equal to create_bitmap(800,600) it gives me the segmentation fault.

Here is the code:

1#include <iostream>
2#include "allegro.h"
3//My headers
4//#include "particlegrid.h"
5//using namespace particles;
6 
7int particles[300][300];
8bool particlemoved[300][300];
9 
10 
11 
12int main()
13{
14 BITMAP* buffer = create_bitmap(800,600);
15
16 allegro_init();
17 install_keyboard();
18 install_mouse();
19
20 set_color_depth(8);
21 set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
22
23
24 set_window_title("Sandman's Dream");
25
26 while(!key[KEY_ESC])
27 {
28
29
30
31
32 }
33}
34END_OF_MAIN()

Kitty Cat
Member #2,815
October 2002
avatar

You're trying to use an Allegro function before Allegro is initialized.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Durnus
Member #7,997
November 2006
avatar

Oh jeez... I feel so stupid. :P

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)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Neil Walker
Member #210
April 2000
avatar

And use an IDE with an integrated debugger :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

HoHo
Member #4,534
April 2004
avatar

... or lots of printf()'s!

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Durnus
Member #7,997
November 2006
avatar

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
avatar

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.

Go to: