From: "Edmund Horner" Newsgroups: comp.os.msdos.djgpp References: <3958A5A3 DOT D470EC39 AT uni-bremen DOT de> Subject: Re: Problem with pointer(?) Lines: 69 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 Organization: Paradise Net Ltd. Customer Message-ID: <962115507.394420@shelley.paradise.net.nz> Cache-Post-Path: shelley.paradise.net.nz!unknown AT 203-79-65-99 DOT tnt8 DOT paradise DOT net DOT nz X-Cache: nntpcache 2.4.0b2 (see http://www.nntpcache.org/) Date: Wed, 28 Jun 2000 02:18:49 +1200 NNTP-Posting-Host: 203.96.152.26 X-Complaints-To: newsadmin AT xtra DOT co DOT nz X-Trace: news.xtra.co.nz 962115525 203.96.152.26 (Wed, 28 Jun 2000 02:18:45 NZST) NNTP-Posting-Date: Wed, 28 Jun 2000 02:18:45 NZST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I have compiled at looked at your program, and it was an interesting case. It appeared to be failing at the first call to printf() ! The problem turns out to be in a place that I was intuitively uncomfortable with: your use of pow(MAX,2) instead of MAX * MAX. You really should rely on floating point function to determine the size of a block of memory! pow(), once int'ized, returned the equivilent of MAX * MAX - 1, so that explains it. You were allocating one less item than you needed to. Let that be a lesson to all of us! Edmund. "Uwe Sydow" wrote in message news:3958A5A3 DOT D470EC39 AT uni-bremen DOT de... > Hello, > I like to calculate a gaussian distribution of numbers > defined by mean and variance on a MAX x MAX grid. > What I don't understand is, for some values of MAX (eg 100) the > programm works, for eg MAX =10 it doesen't. > What did I make wrong? > Thanks a lot for help > > Uwe > > /* prog */ > #include > #include > #include > #include > #define MAX 100 /* 100 is ok, but 10 or 11 crashes ?? */ > int main(void) > > double dd=4.0; /* mean */ > double da=1.0; /* variance */ > short Zh; > int i,j; > double (*tg)[MAX] = calloc(pow(MAX,2), sizeof(double)); > double Z0,Z1,Z2,Z3; > srand(time(0)); > for (i=0;i for (j=0;j Zh=0; > while(Zh==0) { > Z0=(double)rand()/RAND_MAX; > Z1= 2 * dd * Z0; > Z2=(1/sqrt(2*PI*pow(da,2)))*exp(-pow((Z1-dd),2))/(2*pow(da,2)); > Z3=(double)rand()/RAND_MAX; > if( Z2>=Z3 ) > tg[i][j]=Z1; > Zh=1; > } /* end if */ > } /* while */ > } /* for j */ > } /* for i */ > > for (i=0;i printf("\n"); > for (j=0;j printf("%.4f\t",tg[i][j]); > } /* for j */ > } /* for i */ > cfree(tg); > exit(0); > } > /* end of prog */