delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/08/03:45:47

From: G DOT DegliEsposti AT ads DOT it
To: djgpp AT delorie DOT com
Message-ID: <C1256586.002FC2D5.00@vega.ads.it>
Date: Thu, 8 Jan 1998 09:44:58 +0100
Subject: Re: problem with memory allocation (I think)
Mime-Version: 1.0




>I am getting a runtime error when running my program.  Here is the
>part that is not running correctly:
>
>   long i;
>   unsigned int *buffer;
>   buffer = malloc(640 * 480 * 2);
>
>   for(i = 0; i < 640*480; i++)
>      {
>      buffer[i] = i;
>      }
[...]
>
>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 malloc 640 * 480 * 2 bytes, but you are accessing 640 * 480 ints, but
djgpp is a 32 bit compiler, so you need 640 * 480 * 4 bytes to allocate
them all.

If you access 640 * 480 / 2 ints then you are accessing all the memory
you allocate and you have no sigsegv.

ciao
  Giacomo



- Raw text -


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