From: "George Kinney" Subject: Re: General protection fault because of modulus? Newsgroups: comp.os.msdos.djgpp References: <01bc8c6e$c991d480$3564a8c0 AT Kelly DOT ns1 DOT uswest DOT net> Organization: The Unknown Programmers Message-ID: <01bc8e80$de0ec800$fa8033cf@pentium> NNTP-Posting-Host: 207.51.128.250 Date: 12 Jul 97 04:53:51 GMT Lines: 53 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk levity AT minn DOT net wrote in article <01bc8c6e$c991d480$3564a8c0 AT Kelly DOT ns1 DOT uswest DOT net>... > I am writing a program for the purpous of learing how to read binary files. > I am using the modulus fo counting coloms for the out put but every time I > add the code to do this the program runs and then ends with a general > protection fault. Any ideas why I am getting the general protection fault? > Also for future reference should I include my code in a different way when > posting these questions? > > #include > #include > #include ".\bmphead.h" > > int main(int argc, char *argv[]) > { > > long pal[3][256]; [SNIP] > pal[3][i] = palette.Pad; BOOM! [SNIP] > (void)printf("%ld,%ld,%ld,%ld |\n", pal[0][i], pal[1][i], pal[2][i], > pal[3][i]); BLAM! [SNIP] > (void)printf("%ld,%ld,%ld,%ld |", pal[0][i], pal[1][i], pal[2][i], > pal[3][i]); KABLOOEY!!!! This is the infamous off by one bug, sorta. the statement: long pal[3][256]; creates an array that is 3 by 256, this is ok. However, you're code is referring to a *4* by 256 array. Why? C arrays start at 0, and for an array of size N, the last element is N-1, not N. So either change the pal array to [4][256] or get rid of the references to non-existant indexes. Good luck.