Test program with undefined variables runs and gives correct results

I have had a strange experience with a test program, which I report here to seek some light on what the explanation is. The program is about 2,000 lines, with two source files, and three unformatted data files. A zip file with the five files is available. The program can be built with the command “gfortran hwm07.01d.f90 checkhwm07.f90”. Then run the resulting exe in the directory containing the three data files. The program will output nine tables (total 211 lines). These results can be compared to reference results that are in comment lines at the end of checkhwm07.f90. Everything is fine so far.
Now, if we recompile with NAG Fortran’s C=undefined option and run, we see that some variables (array variables) are used without having been defined. In such situations, we expect that the results are not to be trusted. Next, recompile with NAG Fortran without C=undefined and run. The results are fine, again.
Any hints on how an erroneous program can produce correct results? Thanks.

1 Like

A program that violates Fortran rules on only referencing variables that have been defined behaves as if it contains a random number generator.

You are getting “lucky” (or unlucky, because this makes you think that you don’t have to fix the problem and eliminate the RNG).

2 Likes

The devil always lies to you, but sometimes he mixes in some truth to gain your trust. :wink:

The gfortran compiler has options like -finit-real=snan, -ffpe-trap=invalid,zero,overflow, -finit-integer=-999999, (and maybe others) to help identify and locate undefined variables.

1 Like

Thanks. It turned out that one entire two-dimensional array and some elements of another two-dimensional array needed to be initialized to zero. The output results, strangely, were the same as those from the program with these initializations not performed.

Not strangely, expectedly. The most likely value of an uninitialized variable is, plausibly, zero.