Bitmap pointer array + randomiser
Ju-Han Soon

Hi all,
I'm working in Dev-C++ using C. I've got a problem with my array of Bitmap pointers. They are pointing to different bitmap images loaded through datafiles. I've got a function that randomises which pointer array element is pointing to which bitmap image, and its randomising fine with proper results. However, this is causing my program to crash at unload_datafile(data). Any idea why? Partial code below:

1BITMAP *face[5];
2BITMAP *mouth[4];
3BITMAP *eye[3];
4 
5void randomize_elements(BITMAP **ptr_in, int array_len) {
6 BITMAP *temp_ptr;
7 int rand1, rand2;
8 int i = array_len;
9
10 while (i) {
11 rand1 = rand()%array_len;
12 rand2 = rand()%array_len;
13 temp_ptr = ptr_in[rand1];
14 ptr_in[rand1] = ptr_in[rand2];
15 ptr_in[rand2] = temp_ptr;
16 i--;
17 }
18}
19 
20// in main function:
21 data = load_datafile("data.dat");
22 face[0] = data[D_face].dat;
23 mouth[0] = data[D_mouth_0].dat;
24 mouth[1] = data[D_mouth_1].dat;
25 mouth[2] = data[D_mouth_2].dat;
26 mouth[3] = data[D_mouth_3].dat;
27 eye[0] = data[D_eye_0].dat;
28 eye[1] = data[D_eye_1].dat;
29 eye[2] = data[D_eye_2].dat;
30 
31 for (n=1; n<5; n++) {
32 face[n] = create_bitmap(FACE_SIZE, FACE_SIZE);
33 blit(face[0], face[n], 0, 0, 0, 0, FACE_SIZE, FACE_SIZE);
34 }
35
36 floodfill(face[1], FACE_SIZE/2, FACE_SIZE/2, GREENCOL);
37 floodfill(face[2], FACE_SIZE/2, FACE_SIZE/2, BLUECOL);
38 floodfill(face[3], FACE_SIZE/2, FACE_SIZE/2, REDCOL);
39 floodfill(face[4], FACE_SIZE/2, FACE_SIZE/2, YELLOWCOL);
40 
41 randomize_elements(face, 5);
42 randomize_elements(mouth, 4);
43 randomize_elements(eye, 3);
44 
45 unload_datafile(data);

orz

Dunno. Perhaps you should try commenting out things until it stops crashing.

Maikol

First of all, you ought to view if data's NULL; I would even do a cast in:

 face[0] = (BITMAP *) data[D_face].dat
 ...

To ensure it makes a good conversion.

However I think here's the problem: although Allegro stores every datum in dynamic memory, you should not move his pointers as you're doing in:

1 face[0] = data[D_face].dat;
2 mouth[0] = data[D_mouth_0].dat;
3 ...
4 eye[0] = data[D_eye_0].dat;
5 ...
6 
7 // Now they point to somewhere of data, except face[1],
8 // face[2], ...,face[4], which have their own memory. Then, in
9 // randomize_elements(), you assign pointers reffered to data space.
10 
11 void randomize_elements(...)
12 {
13 while (i) {
14 ...
15 temp_ptr = ptr_in[rand1];
16 ptr_in[rand1] = ptr_in[rand2];
17 ptr_in[rand2] = temp_ptr;
18 }
19 }

Maybe Allegro crashes because some faces[...] will point out of his datafile. I recommend you to load ALL data into your own memory; that has never failed me

orz
Quote:

First of all, you ought to view if data's NULL; I would even do a cast in:

face[0] = (BITMAP *) data[D_face].dat
...

To ensure it makes a good conversion.

Double checking the type is probably a good idea, but a cast like that is strictly a static, compile time thing - the only thing it could effect is compile time errors or warnings. To double-check the validity of those pointers, you need to check (data[whatever].type == DAT_BITMAP) instead.

Quote:

However I think here's the problem: although Allegro stores every datum in dynamic memory, you should not move his pointers as you're doing in:
snip
Maybe Allegro crashes because some faces[...] will point out of his datafile. I recommend you to load ALL data into your own memory; that has never failed me

There are potential issues with the datafiles allocations yes, but in the code he showed, he never modifies anything that came out of a data file, only pointers at them. Unless I'm missing something.

Dennis

Does it really crash at unload_datafile or perhaps at some point later, when you try to access any of face,mouth,eye?
(that would be nothing unusual then, because after unload_datafile all pointers in face,mouth,eye would just point to any memory garbage)

Ju-Han Soon

My workaround solution would be to load a single bitmap that has all the face, mouth and eye as tiles and grab them. That should turn out good. But its just stumping to have a problem like this with a simple construct but yet seems buggy.

And unload_datafile is the 2nd last command in my main(), before set_gfx_mode(GFX_TEXT...)

orz

It really is simple enough that commenting out pieces ought to be informative. Does it crash with just load/unload datafile? Perhaps you could upload enough to compile and run?

Ju-Han Soon

The reason I know that its the randomizer function thats causing problems is that commenting it out solves my problem.

Right now, I'm loading the pictures into memory and removing the datafile. All is well.

Thread #558930. Printed from Allegro.cc