Array Bound Checker

Array Bound Checker

Hello,

Is there a way to give an error message when the code tries and accesses
an element of an array that is out of the bounds of the array?

So, for example, if length is of size 5, and the code tries to access length(6), an
error message would appear at compile time.

Thank you for your time and assistance!

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

No, not at compile time. The -check bounds option enables a run-time check for out of bounds references.

Actually, -check bounds will also do some compile-time checking for "obvious" cases. For example:

integer array(5)

array = 0
print *, array(6)
end

C:Projects>ifort /check:bounds t.f90
Intel Visual Fortran Compiler Professional for applications running on IA-32,
Version 11.1 Build 20090903 Package ID: w_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

t.f90(5): error #5560: Subscript #1 of the array ARRAY has value 6 which is greater than the upper bound of 5
print *, array(6)
---------^

(I did this on Windows but it will be the same on Linux.) I will comment that it is rare that the Fortran source will be so blatant about the indexing error as to allow the compiler to catch this at compile time, but it can do so.

Retired 12/31/2016

Quoting - Steve Lionel (Intel)
Actually, -check bounds will also do some compile-time checking for "obvious" cases. For example:

integer array(5)

array = 0
print *, array(6)
end

C:Projects>ifort /check:bounds t.f90
Intel Visual Fortran Compiler Professional for applications running on IA-32,
Version 11.1 Build 20090903 Package ID: w_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

t.f90(5): error #5560: Subscript #1 of the array ARRAY has value 6 which is greater than the upper bound of 5
print *, array(6)
---------^

(I did this on Windows but it will be the same on Linux.) I will comment that it is rare that the Fortran source will be so blatant about the indexing error as to allow the compiler to catch this at compile time, but it can do so.

Hi Kevin and Steve,

Thank you very much for your help.

I used the option and it worked like a charm!

Thanks again!

On a side note, 'length' is not a very good variable name, is it.

Following on the previous discussion, what I am missing here?

When I explicitly access elements which are out of the array, I get neither compiler nor run-time errors.

Notice that I use the -check bounds flag when compiling the program.

pmginacio@hpc03 ~/.tmp/testbounds
$ ifort --version
ifort (IFORT) 13.1.3 20130607
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

pmginacio@hpc03 ~/.tmp/testbounds
$ cat ./testbounds.f90
! Template program
program testbounds

implicit none

!variables declaration
real(8) :: dcm(9)
integer :: i

! inits
do i=1,size(dcm)
    dcm(i)=i
end do

print *, dcm(6:14)

end program testbounds
pmginacio@hpc03 ~/.tmp/testbounds
$ ifort -check bounds -o testbounds testbounds.f90
pmginacio@hpc03 ~/.tmp/testbounds
$ ./testbounds
   6.00000000000000        7.00000000000000        8.00000000000000     
   9.00000000000000       0.000000000000000E+000  0.000000000000000E+000
  0.000000000000000E+000  2.121995790965272E-314  1.943555437610296E-316

It looks as if the array bounds check doesn't happen when the subscript is part of an I/O list. It really should and I will let the developers know.

Retired 12/31/2016

Array bounds checking for I/O lists has been implemented for a future (2016) major release.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today