20. Debugging hints

It has been estimated that about 90% of the time it takes to develop commercial software is spent debugging and testing. This shows how important it is to write good code in the first place.

Still, we all discover bugs from time to time. Here are some hints for how to track them down.

Useful compiler options

Most Fortran compilers will have a set of options you can turn on if you like. The following compiler options are specific to the Sun Fortran 77 compiler, but most compilers will have similar options (although the letters may be different).

-ansi
This will warn you about all non-standard Fortran 77 extensions in your program.
-u
This will override the implicit type rules and make all variables undeclared initially. If your compiler does not have such an option, you can achieve almost the same thing by adding the declaration
      implicit none (a-z)

in the beginning of each (sub-)program.

-C
Check for array bounds. The compiler will try to detect if you access array elements that are out of bounds. It cannot catch all such errors, however.

Some common errors

Here are some common errors to watch out for:

Debugging tools

If you have a bug, you have to try to locate it. Syntax errors are easy to find. The problem is when you have run-time errors. The old-fashioned way to find errors is to add write statements in your code and try to track the values of your variables. This is a bit tedious since you have to recompile your source code every time you change the program slightly. Today one can use special debuggers which is a convenient tool. You can step through a program line by line or define your own break points, you can display the values of the variables you want to monitor, and much more. Most UNIX machines will have the debuggers dbx and gdb. Unfortunately, these are difficult to learn since they have an old-fashioned line-oriented user interface. Check if there is a graphical user interface available, like, e.g., xdbx or dbxtool.

Copyright © 1995-7 by Stanford University. All rights reserved.