Sender: nate AT cartsys DOT com Message-ID: <3587280D.E555624B@cartsys.com> Date: Tue, 16 Jun 1998 19:21:01 -0700 From: Nate Eldredge MIME-Version: 1.0 To: e96pd AT efd DOT lth DOT se CC: djgpp AT delorie DOT com Subject: Re: Help! Why is my program crashing? References: <35867E83 DOT E01 AT efd DOT lth DOT se> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Precedence: bulk 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); > } > void reset(int *m,int W2)//set to zero. Here it crashes > { > for(int a5=0;a5 for(int q=0;q m[(long)a5*W2+q]=0; > } > void main(int argc, char *argv[]) > { > initmapheight(mapheight); > reset(mapheight,100); > } C uses pass-by-value. Thus, when you assign the result of `calloc' to `n', only the local copy is changed, and nothing happens to `mapheight'. To do it right, you must pass `mapheight's address: void initmapheight(int **n) { *n = (int *)calloc(whatever); if (!*n) exit(1); } int *mapheight; ... initmapheight(&mapheight); Note also that `main' must return `int'. -- Nate Eldredge nate AT cartsys DOT com