delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/26/11:46:37

From: Thomas Demmer <demmer AT LSTM DOT Ruhr-UNI-Bochum DOT De>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem with malloc() and integers
Date: Sun, 26 Apr 1998 17:44:41 +0200
Organization: Lehrstuhl fuer Stroemungsmechanik
Lines: 38
Message-ID: <35435669.6BA11DAA@LSTM.Ruhr-UNI-Bochum.De>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 980426121334 DOT 19566U-100000 AT is> <35433669 DOT 5A75 AT bluewin DOT ch>
NNTP-Posting-Host: bvb.lstm.ruhr-uni-bochum.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

leger_v AT bluewin DOT ch wrote:
> 
> Here's the source code of the whole program :
> 
> 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.
> Thanx
No surprise. you malloc 64000 bytes, but
write 4*64000 bytes. Rather say
  test[i]=(unsigned long*)malloc(64000*sizeof(unsigned long));

Anyway, it is always a good idea to write that like
  foo=malloc(n*sizeof(*foo));

-- 
Ciao
Tom

*************************************************************
* Thomas Demmer                                             *
* Lehrstuhl fuer Stroemungsmechanik                         *
* Ruhr-Uni-Bochum                                           *
* Universitaetsstr. 150                                     *
* D-44780  Bochum                                           *
* Tel: +49 234 700 6434                                     *
* Fax: +49 234 709 4162                                     *
* http://www.lstm.ruhr-uni-bochum.de/~demmer                *
*************************************************************

- Raw text -


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