Mail Archives: djgpp/1996/08/29/11:57:29
On Tue, 27 Aug 1996, Alex Demko wrote:
>=A6PALETTE *new_truepal() =20
>=A6{ =20
>=A6 PALETTE *pal =3D (PALETTE *)malloc(sizeof(PALETTE)); =20
>=A6 unsigned char r,g,b,i=3D0; =20
>=A6 for (r=3D0; r<6; r++) =20
>=A6 for (g=3D0; g<6; g++) =20
>=A6 for (b=3D0; b<6; b++) =20
>=A6 { =20
>=A6 pal[i]->r=3Dr*12;//causes GPF here =20
>=A6 pal[i]->g=3Dg*12; =20
>=A6 pal[i]->b=3Db*12; =20
>=A6 pal[i]->filler=3D0; =20
>=A6 i++; =20
>=A6 } =20
>=A6 return pal; =20
>=A6} =20
>
>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 problem here is that you misunderstood what the PALETTE type is. It
defines just ONE entry in the palette table, not the ENTIRE array. So calli=
ng
malloc as you did above allocates an array with ONE entry (or just one obje=
ct
of the PALETTE type, if you will). Then, when trying to access the next mem=
ory
location at which you think the next palette entry is, you hit an
undefined/uncommited region which lies in the middle of nowhere and causes
your program to GP-fault.
The solution is simple:
=20
PALETTE* pal =3D (PALETTE*)malloc(sizeof(PALETTE) * palette_entries_count);
Where palette_entries_count is variable/literal/constant/etc which specifie=
s
how many palette entries you want allocated.
=20
>
>The code compiled error free.
There was no error in your code ;-)))))
>Thanks
Anytime
Mark
/************************************************************/
/** Maybe it was infatuation or the thrill of a chase? **/ =20
/** Maybe you were always beyond my reach and my heart **/
/** was playing safe? ***********/=20
/** But was that love in your eyes I saw, **/
/** or the reflection of mine? **/
/** I'll never really know for sure, **/
/** You never really gave me time! **/
/** Won't you give me that time? **/=20
/** "Cindirella Search" **/
/********************************/
Visit my homepage: http://ananke.amu.edu.pl/~grendel
- Raw text -