Mail Archives: djgpp/1998/06/16/22:25:21
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<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);
> }
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
- Raw text -