www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/16/23:10:52

From: Fabrice ILPONSE <fabrice AT asim DOT lip6 DOT fr>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help! Why is my program crashing?
Date: Tue, 16 Jun 1998 16:20:04 +0200
Organization: Universites Paris VI/Paris VII - France
Lines: 56
Message-ID: <35867F14.6D9AD19F@asim.lip6.fr>
References: <35867E83 DOT E01 AT efd DOT lth DOT se>
NNTP-Posting-Host: asim.lip6.fr
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Peter Danielsson wrote:
> 
> Why does my program crash all the time. It's a simple program, only a
> few lines:
> 
> int WIDTH=513;
> int *mapheight;
> 
> void initmapheight(int *n)//allocateing memory
> {
>  n=(int*)calloc((long)513*513,sizeof(int));
>  if(n==NULL)
>          exit(-1);
> }

	You allocates n in this function but it do not change the parameter
pointer outside of the function.
do:

void initmapheight(int **n)//allocateing memory
 {
  *n=(int*)calloc((long)513*513,sizeof(int));
  if(*n==NULL)
          exit(-1);
 }

or much better:

int *initmapheight()//allocateing memory
 {
   int *n=(int*)calloc((long)513*513,sizeof(int));
   return n;

 }
and test for NULL value.
	
> void reset(int *m,int W2)//set to zero. Here it crashes
> {
>  for(int a5=0;a5<W2;a5++)
>          for(int q=0;q<W2;q++)
>                 m[(long)a5*W2+q]=0;
> }
> void main(int argc, char *argv[])
> {
> initmapheight(mapheight);
> reset(mapheight,100);
> }

-- 
	^ ^ ^
	| | |
	+-+-+	Fabrice ILPONSE
	  |	email: fabrice AT asim DOT lip6 DOT fr
	  |
	  |
	  -

- Raw text -


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