Mail Archives: djgpp/1998/06/18/10:37:22
Peter Danielsson <e96pd AT efd DOT lth DOT se> wrote in article
<35867E83 DOT E01 AT efd DOT lth DOT se>...
> 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)); --- Here You assign n, not
mapheight!
> if(n==NULL)
> exit(-1);
> }
Param n is a local copy of a pointer to int. Assign n alter the local copy
only. To change the original parameter, You have to pass the pointer to it,
that is:
void initmapheight(int **n)
{
*n=(int *)calloc(...) // assign *n, that is *(&mapheight) => mapheight
...
}
> void main(int argc, char *argv[])
> {
> initmapheight(&mapheight); // pass address of the pointer
> reset(mapheight,100);
> }
>
In your original code, mapheight still point to NULL. and progra crash.
(Note that C++ has the type "reference" that can be used to pass read/write
arguments.)
- Raw text -