Mail Archives: djgpp/1998/01/08/04:01:06
On Thu, 8 Jan 1998, David Eberhard wrote:
> If I change the for loop so that it only loops 640*480/2 times, then
> it works fine. Can anyone tell me what's going on?
You are assuming that the size of an int is 2 bytes, but in DJGPP it is 4
bytes (32 bits), and thus your loop writes beyond the limits of the buffer
you allocated. You should not do any assumptions like that; use the
sizeof operator instead. The following line:
> buffer = malloc(640 * 480 * 2);
should have been written like this:
buffer = (unsigned int *)malloc(640 * 480 * sizeof(int));
Also, please note that it is usually a bad idea to put a long into a
buffere which was malloc'ed for int's, like you do:
long i;
unsigned int *buffer;
...
buffer[i] = i;
This works in DJGPP, since int and long are both 32-bit, but on other
architectures it will crash.
- Raw text -