Debugging the Squares Example Program

The following program (SQUARES) uses the Fortran Console project type. The SQUARES program reads formatted data from the file datafile.dat and displays the calculated results. With the source code shown below, it does not generate the expected results:

    PROGRAM SQUARES
       INTEGER INARR(10), OUTARR(10), I, K
 ! Read the input array from the data file.
       OPEN(UNIT=8, FILE='datafile.dat', STATUS='OLD')
       READ(8,*,END=5) N, (INARR(I), I=1,N)
   5   CLOSE (UNIT=8)

 ! Square all nonzero elements and store in OUTARR.
       K = 0
       DO I = 1, N
	 IF (INARR(I) .NE. 0) THEN
 ! Is the error in this DO loop?
	    OUTARR(K) = INARR(I)**2
	 ENDIF
       END DO

 ! Print the squared output values.  Then stop.
       PRINT 20, N
   20  FORMAT (' Total number of elements read is',I4)
       PRINT 30, K
   30  FORMAT (' Number of nonzero elements is',I4)
       DO, I=1,K
         PRINT 40, I, OUTARR(K)
   40    FORMAT(' Element', I4, ' has value',I6)
       END DO
   END PROGRAM SQUARES

The formatted file datafile.dat currently contains one record that includes the following:

To view the values of this formatted data file in the Microsoft visual development environment, use the Open item in the File menu.

When executed without array bounds checking (set by the /check:nobounds option), the output appears as follows:

output from original program execution, /check:nobounds

When the program was executed with array bounds checking on, the output appears as follows:

output from original program execution, /check:bounds

You can either build this program from the command line or within the visual development environment (see Preparing Your Program for Debugging). This example assumes a project workspace already exists.

To debug this program:

  1. From the Compaq Visual Fortran program folder, click Developer Studio to start the visual development environment.
  2. In the File menu, click Open Workspace.
  3. Click the FileView pane in the Workspace window. If the Workspace window is not displayed, click Workspace in the View menu.
  4. Edit the file squares.f90: double-click its file name in the FileView pane. The screen appears as follows:

    visual development environment, after opening project workspace

    The following toolbars are shown:

    To change the displayed toolbars, select Customize in the Tools menu and click the Toolbars tab. You can move toolbars by dragging the anchor (double vertical line on the left of the toolbar).

  5. Click the first executable line to set the cursor position. In this case, click the beginning of the OPEN statement line:
       OPEN(UNIT=8, FILE='datafile.dat', STATUS='OLD')
    
  6. Click the Set/Remove Breakpoint (open hand symbol) button in the Build toolbar:

    set initial the breakpoint, build toolbar

    The red circle in the left margin of the text editor/debugger window shows where a breakpoint is set.

  7. This example assumes you have previously built your application (see Preparing Your Program for Debugging).

    In the Build menu, click the Start Debug, Go item:

    starting the debugger, build menu

  8. The debugger is now active. The current position is marked by a yellow arrow at the first executable line (the initial breakpoint):

    debugger positioned at the first breakpoint

    The Debug menu appears on the visual development environment title bar in place of the Build menu. If not displayed previously, the Debug toolbar appears.

    If needed, you can set another breakpoint, position the cursor at the line where you want to add or remove a breakpoint and do either of the following:

    Step through the lines of source code. You can do this with the Debug menu item Step Over (as shown) or the Step Over button on the Debug toolbar:

    debug menu, step over item

  9. Repeat the Step Over action and follow program execution into the DO loop. Repeat the Step Over action until you are at the end of the program. Position the cursor over the variable K to view its value (called Data Tips):

    viewing the value of variable k

    The error seems related to the value of variable K!

  10. In the text editor, add the line K = K + 1 as follows:
     ! Square all nonzero elements and store in OUTARR.
           K = 0
           DO I = 1, N
             IF (INARR(I) .NE. 0) THEN
             K = K + 1       ! add this line
             OUTARR(K) = INARR(I)**2
             ENDIF
           END DO
    
  11. You have modified the source, so you need to rebuild the application:

    The output screen appears as follows:

    displayed output after fixing the program

  12. Although the program generates better results, you can examine the values of both the input array INARR (read from the file) and the output array OUTARR that the program calculates. In the text editor window, the previously set breakpoint remains set.

    In the Build menu, click the Start Debug, Go item.

  13. To view the values of certain variables as the program executes, we need to display the Variables or the Watch window. In the View menu, click the Debug Windows, Variables window item:

    display the debug variables window: view menu, debug windows, variables

  14. In the Variables window, click the Locals tab to display the values of your local variables:

    display the locals tab in the variables window

    You can view the values of the local variables by using the Locals tab, including the arrays (click the plus sign).

    The Variables window displays a Context menu (after the word Context:). The Context menu can help you debug exceptions.

    The Locals tab does not let you display module variables or other non-local variables. To display non-local variables, display the Watch window:

    view menu, debug windows, watch: displaying the watch window

  15. Although this example does not use module variables or non-local variables, you can drag a variable name into the Watch window so the variable can be displayed. The Watch window allows you to display expressions.

    In the text editor window, select the variable name INARR (without its subscript syntax), drag it, and drop it into the Watch window:

    select inarr in text window. drag and drop to watch window

  16. Also drag the OUTARR array name to the Watch window. Click the Plus sign (+) to the left of the OUTARR variable's name to display the values of its array elements.
  17. Execute lines of the program by using the Step Over button on the Debug toolbar. As the program executes, you can view the values of scalar variables with the data tips feature and view the values of arrays (or other variables) in the Watch window.

    When the program completes execution, the screen appears as follows:

    watch window at completion of corrected program

If a Disassembly window (shows disassembled code with source-code symbols) unintentionally appears, click the Step Out button on the debugger toolbar (or select the Step Out item in the Debug menu) as needed to dismiss the Disassembly window.

If you have the Visual Fortran Professional or Enterprise Edition, you can use the Array Viewer to display and graph multidimensional array element values.

For more information: