Visible to Intel only — GUID: GUID-9284118A-1D27-4C31-B729-45DBFA2106AC
Visible to Intel only — GUID: GUID-9284118A-1D27-4C31-B729-45DBFA2106AC
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
implicit none
INTEGER INARR(10), OUTARR(10), I, K, N
! initialize the input array and number of elements (4).
N = 4
INARR(1:4) = (/ 3,2,5,2 /)
! 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(I)
40 FORMAT(' Element', I4, 'Has value',I6)
END DO
END PROGRAM SQUARES
The program SQUARES performs the following functions:
Initializes 4 numbers in the array INARR. The array INARR contains the integer values 4, 3, 2, 5, and 2. The variable K holds the number of data items in INARR, which is 4.
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 Prepare Your Program for Debugging and Use Breakpoints in the Microsoft Debugger). This example assumes a solution already exists.
To debug this program:
Start Visual Studio*.
In the File menu, select Open Solution. Open the solution containing the file.
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.
To change the displayed toolbars, select View > Toolbars. To display the debug toolbar, select Debug.
Click the first executable line to set the cursor position. In this case, click the beginning of the assignment statement line for N:
N = 4
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:
This example assumes you have previously built your application (see Prepare 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).
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.
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.
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.
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
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:
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.
To view the values of certain variables as the program executes, display the Locals window. In the Debug menu, select Windows > Locals.
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.
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:
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.
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.