Mail Archives: djgpp/1997/07/13/11:31:34
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 <stdio.h>
> #include <stdlib.h>
> #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.
- Raw text -