Xref: news2.mv.net comp.os.msdos.djgpp:7992 From: "Mike Marcelais" Newsgroups: comp.os.msdos.djgpp Subject: Re: djgpp/allegro - help! - quick question Date: 28 Aug 1996 22:25:55 GMT Organization: Microsoft Corporation Lines: 62 Message-ID: <01bb9530$89683f60$ba27379d@michmarc2> References: <3223B9FC DOT 5C79 AT mbnet DOT mb DOT ca> <322464ED DOT 167EB0E7 AT eng DOT umd DOT edu> NNTP-Posting-Host: 157.55.39.186 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp John Sabean 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 =--+