delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/16/18:00:42

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help! Why is my program crashing?
Date: Tue, 16 Jun 1998 21:33:54 GMT
Organization: Customer of EUnet Austria
Lines: 51
Message-ID: <3595e290.5876629@news.Austria.EU.net>
References: <35867E83 DOT E01 AT efd DOT lth DOT se>
NNTP-Posting-Host: e190.dynamic.vienna.at.eu.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: Peter Danielsson <e96pd AT efd DOT lth DOT se>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Tue, 16 Jun 1998 16:17:39 +0200:

>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);
>}

If you want mapheight to hold the pointer allocated in initmapheight() you
must make it a int **n. Otherwise the memblock you allocate goes the way of
all used up bytes and disappears in the depths of the stack. Or better yet you
return it.

int *initmapheight(void)
{
   int *n;
    if((n = calloc(513L, sizeof(int))) == NULL)
       exit(-1);

   return(n);
}

--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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