Mail Archives: djgpp/1996/08/29/00:25:12
John Sabean <docmani AT eng DOT umd DOT edu> wrote in article
<322464ED DOT 167EB0E7 AT eng DOT umd DOT edu>...
| Alex Demko wrote:
| >
| > ¦PALETTE *new_truepal()
| > ¦{
| > ¦ PALETTE *pal = (PALETTE *)malloc(sizeof(PALETTE));
| > ¦ unsigned char r,g,b,i=0;
| > ¦ for (r=0; r<6; r++)
| > ¦ for (g=0; g<6; g++)
| > ¦ for (b=0; b<6; b++)
| > ¦ {
| > ¦ pal[i]->r=r*12;//causes GPF here
| > ¦ pal[i]->g=g*12;
| > ¦ pal[i]->b=b*12;
| > ¦ pal[i]->filler=0;
| > ¦ i++;
| > ¦ }
| > ¦ return pal;
| > ¦}
| >
| > After I use the above routine in my code, I get a GPF in the marked
| > area. It won't GPF the first time it hits the marked code area, but
| > it will interate the inner loop about 40 (actual number depends on
| > phase of the moons) times, then GPFs. Am I dereferencing the
| > array/struct wrong? Please help, this thing is killing me :)
| >
| > The code compiled error free.
| >
| > Thanks
| >
|
| Perhaps since you've only allocated one palette structure and
| you're trying to access more than one?
No...but close. :-)
|
| Look at the allegro.h file. You'll find the line:
| >typedef RGB PALLETE[PAL_SIZE];
A PALLETTE is an array of RGB's. sizeof(PALLETTE) will be
sizeof(RGB)*PAL_SIZE so he's mallocing the right stuff. However, the problem
is that after you malloc a PALLETTE like that you -don't- have a PALLETE *,
you have an RGB *.
Trying to access PALLETE[1] will put you past the end of the PALLETE (since
you don't have an array of PALLETTE) and is the same as (pallete +
sizeof(PALLETE)) {using byte addition}
The fix is to change the malloc line to be `RGB *pal = (RGB
*)malloc(sizeof(PALLETE));' and your problem will go away.
--
+------------------------+----------------------+
| Mike Marcelais | Excel Developer and |
| michmarc AT microsoft DOT com | Magic Rules Guru |
+------------------------+----------------------+
| Opinions expressed in this post are mine, and |
| do not necessarily reflect those of Microsoft |
+--= Moonstone Dragon =---------------= UDIC =--+
- Raw text -