Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Debug the Squares Example with Microsoft Debugger

The following example shows a program called SQUARES that requires debugging. The program was compiled and linked without diagnostic messages from either the compiler or the linker, however, this program contains a logic error in an arithmetic expression.

   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
        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 program SQUARES performs the following functions:

  • Reads a sequence of integer numbers from a data file and saves these numbers in the array INARR. The file datafile.dat contains one record with the integer values 4, 3, 2, 5, and 2. The first number indicates the number of data items that follow.

  • Enters a loop in which it copies the square of each nonzero integer into another array OUTARR.

  • Prints the number of nonzero elements in the original sequence and the square of each such element.

In the program, the logic error occurs because variable K, which keeps track of the current index into OUTARR, is not incremented in the loop on lines 9 through 13. The statement K = K + 1 should be inserted just before line 11.

This example assumes that the program was executed without array bounds checking (set by specifying nobounds for the check command-line option). When executed with array bounds checking, a runtime error message appears.

Debug the Squares Program

The following steps show how to debug a Windows*-based program using the Microsoft integrated debugger within the integrated development environment (see Preparing Your Program for Debugging and Using Breakpoints in the Debugger). This example assumes a solution already exists.

To debug this program:

  1. Start Visual Studio*.

  2. In the File menu, select Open Solution. Open the solution containing the file.

  3. Edit the file squares.f90: double-click the file name in the Solution Explorer View. The screen appears as follows:



    The following toolbars are shown: Build toolbar, Standard toolbar.

  4. To change the displayed toolbars, select View > Toolbars. To display the debug toolbar, select Debug.

  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 in the left margin of the first executable line to set a breakpoint. 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 Debug menu, select Start Debugging.

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

  8. If needed, you can set another breakpoint. Position the cursor at the line where you want to add a breakpoint, right click, and select Breakpoint > Insert Breakpoint.

  9. Step through the lines of source code. You can do this with Debug > Step Over or with the Step Over button on the Debug toolbar.

  10. Repeat the Step Over action and follow program execution into the DO loop and unto the end of the program. Position the cursor over the variable K to view its value in a Data Tip box:



    The error seems related to the value of variable K.

  11. 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
  12. Since you have modified the source, you need to rebuild the application:

    • In the Debug menu, select Stop Debugging

    • In the Build menu, select Build debugtest.

    • In the Debug menu, select Start Debugging.

    The output screen appears as follows:



  13. The program now 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 Debug menu, select Start Debugging.

  14. To view the values of certain variables as the program executes, display the Locals window. In the Debug menu, select Windows > Locals.

  15. You can view the values of the local variables in the Locals window. Click the plus sign to view array values.



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

  16. 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, Name column:

  17. 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.

  18. 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.

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