next up previous contents
Next: Array Inquiry Intrinsics Up: Arrays Previous: Array I/O Example

 

Array Bound Violations

Attempting to reference beyond the end of an array is one of the commonest errors in Fortran programming; array subscript references must lie within the bounds declared for an array. Compilers rarely check for this by default although many provide a switch for this purpose (for example, -CB on the EPC compiler, -C on the NAg compiler).

The following example demonstrates a possible bound violation (if M tex2html_wrap_inline27940 ):

    REAL, DIMENSION(50) :: A
     ...
    DO I=1,M
      A(I) = A(I-1) + 1.0 ! refs A(0)
    END DO

A bound violation may or may not cause the program to crash. In the example above, what will happen is that the reference A(0) will access the bit pattern that is resident in the memory location just before the start of the array. If the contents of this location can be interpreted as a REAL number then this value will be used (whatever it may be), otherwise the program will crash. It is quite likely that the value of this location will be different on each run of the program. Array bound errors are often very difficult to spot! Therefore, it is recommended that during development work array bound checking should always be switched on (along with the generate debug information switch, -g, if available) -- this will alert the user to inconsistencies, however, bound checking will usually slows the code down noticeably.

Return to corresponding overview page gif


next up previous contents
Next: Array Inquiry Intrinsics Up: Arrays Previous: Array I/O Example

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.