Mail Archives: djgpp/1999/04/19/09:07:34
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 -