www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/18/10:37:22

From: "Alberto Chessa" <achessa AT fiar DOT finmeccanica DOT it>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help! Why is my program crashing?
Date: 17 Jun 1998 06:43:47 GMT
Organization: Telecom Italia Net
Lines: 36
Message-ID: <01bd99bb$998b01c0$92c809c0@chessa>
References: <35867E83 DOT E01 AT efd DOT lth DOT se>
NNTP-Posting-Host: a-mi23-62.tin.it
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


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