www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/19/09:07:34

Date: Mon, 19 Apr 1999 16:06:23 +0200
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Message-Id: <199904191406.QAA06845@acp3bf.physik.rwth-aachen.de>
To: sydow AT uni-bremen DOT de
Cc: djgpp AT delorie DOT com
Subject: Re: How to optimize that code?
Newsgroups: comp.os.msdos.djgpp
Organization: RWTH Aachen, III. physikalisches Institut B
X-Newsreader: TIN [version 1.2 PL2]
Reply-To: djgpp AT delorie DOT com

In article <371B172C DOT 4771 AT uni-bremen DOT de> you wrote:

> I wrote a piece of code that iterates an harmonic function.
> If I increase MAX to lets say 50000 I obtain runtime errors.
> (compiled with DJGPP gcc 2.8.0 under w95)

You don't show us the runtime errors, but even so, it's clear what
happened. Actually, it's a FAQ, and answered in faq*b.zip.  In a
nutshell, your automatic array variable 'x' is too large. It overflows
the (fixed) stack size.

> I think there are more elegant ways of writing this code, but,
> for instance, I am not very familiar with pointers (if they were
> needed...). 

You'll definitely have to become familiar with pointers if you're
willing to get anywhere with C or C++. 

See changes to the source, below (un-quoted lines...)

> /*    			 starts here     */
> #include<stdio.h>
> #define MAX 10000
> int main(void)
> {
> 	long double x[MAX+3] = {0.0,1.0};

make that line
	long double *x = malloc((MAX+3)*sizeof(*x));

> 	long double c = 0.01;
> 	long int t;

and move the initialization here:

	x[0]=0.0;
	x[1]=1.0;

> 	for (t = 0; t <= MAX; t++) {
> 		x[t + 2] = (2*x[t + 1] - x[t] - c*x[t + 1]);
> 		printf("%.10Lf\n", x[t]);
> 		}
> 	return(0);
> }
> /*             end          */

--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


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