delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/08/04:01:06

Date: Thu, 8 Jan 1998 11:00:39 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: David Eberhard <toothpck AT xmission DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: problem with memory allocation (I think)
In-Reply-To: <34B47DFA.E0D6D796@xmission.com>
Message-ID: <Pine.SUN.3.91.980108105452.8789O-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019