delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/26/12:15:24

Date: Sun, 26 Apr 1998 19:13:48 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: leger_v AT bluewin DOT ch
cc: djgpp AT delorie DOT com
Subject: Re: Problem with malloc() and integers
In-Reply-To: <35433669.5A75@bluewin.ch>
Message-ID: <Pine.SUN.3.91.980426191152.21176C-100000@is>
MIME-Version: 1.0

On Sun, 26 Apr 1998 leger_v AT bluewin DOT ch wrote:

> void main(void)
> { int i,j;
>   unsigned long *test[10];
> 
>   for(i=0;i<10;i++)
>   test[i]=(unsigned long *)malloc(64000);
> 
>   for(i=0;i<10;i++)
>   for(j=0;j<64000;j++)
>   test[i][j]=10;
> }
> And this program crashes on my computer. 

Of course, it will crash!  "malloc(64000)" allocates 64K BYTES, not 64K
LONGS, so you are referencing in the loop more than you have allocated! 
You need to say "malloc(64000*sizeof(long))" to get what you want without
crashing. 

- Raw text -


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