fpt and WinFPT Reference Manual - Command-line Commands

| SimCon Home | Ref Manual Home |

CHECK ARRAY BOUNDS and INSERT ARRAY BOUNDS CHECKS

Syntax:

[DO NOT] CHECK [ARRAY] BOUNDS [DO NOT] INSERT [ARRAY] BOUNDS CHECK[S]

Function:

fpt checks that array indices are within the bounds declared for the array. There are six possible situations:

Inserting Bounds Checks in the Code

In the statement:

n = arr(i,j)

where arr is declared arr(2,10), the variables i and j are of type INTEGER*4 and both indices are to be checked, fpt inserts calls to the fpt library routine fpt_check_bounds_i4 as shown below:

CALL fpt_check_bounds_i4(1234, i, 1, 2) CALL fpt_check_bounds_i4(1235, j, 1, 10) n = arr(i,j)

where 1234 and 1235 are unique identifiers for the bounds checks, and 1, 2 and 1, 10 are the bounds against which i and j are checked. The library contains bounds check routines for 1, 2, 4 and 8-byte integers and 4 and 8-byte reals.

Occasionally a situation arises where a bounds check cannot be inserted as a separate statement. For example:

IF (arr(i,j) > 5) THEN ! 1 WRITE(6,'("Detected arr(i,j) > 5")') ELSEIF (arr(i,j) < 1) THEN ! 2 WRITE(6,'("Detected arr(i,j) < 1")') ENDIF

In this situation the array reference at 2 cannot be checked by inserting a separate statement. The code is modified:

CALL fpt_check_bounds_i4(1236, i, 1, 2) CALL fpt_check_bounds_i4(1237, j, 1, 10) IF (arr(i,j) > 5) THEN ! 1 WRITE(6,'("Detected arr(i,j) > 5")') ELSEIF (arr(fpt_check_bounds_i4_fun(1238, i, 1, 2), & fpt_check_bounds_i4_fun(1239, j, 1, 10)) < 1) THEN ! 2 WRITE(6,'("Detected arr(i,j) < 1")') ENDIF

Again a set of functions is supplied for different integer and real kinds. The value returned by each function is the value of the index tested. Declarations of the functions are inserted automatically where they are needed.

The bounds check subroutines and functions do not halt the program in the same way as a bounds check inserted by a compiler. They simply increment a counter within an array which is indexed by the unique identifier for each check. This array is initialised to zero by the fpt library subroutine fpt_init_bounds_check and a call to this routine is inserted at the start of the main program. The non-zero elements of this array are reported by the subroutine fpt_write_bounds_check and calls to this routine are inserted at every exit point from the program. The results are written to the file fpt_bounds_check.txt. This is convenient if significant work is involved in setting up a program to make a bounds check. All of the out-of-bounds references can be caught in one program run.

Recommended use of the INSERT ARRAY BOUNDS CHECKS Command

Add the fsp file FPTMAIN:insert_bounds_checks.fsp at the end of the fsp or fpp file for the project. This file contains the INSERT ARRAY BOUNDS CHECKS command and the appropriate library references.

Where to use the CHECK ARRAY BOUNDS command:

Operating system command line Yes
Configuration file, config.fsp Yes
Specification (fsp) files, *.fsp Yes
Interactively, to FPT> prompt Yes
Interactive command files Yes
Embedded in the Fortran code Yes

Where to use the INSERT ARRAY BOUNDS CHECKS command:

Operating system command line Yes
Configuration file, config.fsp Yes
Specification (fsp) files, *.fsp Yes
Interactively, to FPT> prompt Yes
Interactive command files Yes
Embedded in the Fortran code Yes

Default

Array bounds with constant indices are checked by default. Those with variable indices are not, and no bounds check is inserted by default (Note that defaults may be changed in the configuration file).

Copyright ©1995 to 2024 Software Validation Ltd. All rights reserved.