Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Array Bound Checker

seniorrojo
初学者
2,876 次查看
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!

0 项奖励
7 回复数
Kevin_D_Intel
员工
2,876 次查看

No, not at compile time. The -check bounds option enables a run-time check for out of bounds references.
0 项奖励
Steven_L_Intel1
2,876 次查看
Actually, -check bounds will also do some compile-time checking for "obvious" cases. For example:

[plain]integer array(5)

array = 0
print *, array(6)
end[/plain]
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.
0 项奖励
seniorrojo
初学者
2,876 次查看
Actually, -check bounds will also do some compile-time checking for "obvious" cases. For example:

[plain]integer array(5)

array = 0
print *, array(6)
end[/plain]
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!


0 项奖励
Sveta
初学者
2,876 次查看

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

0 项奖励
Pedro_I_
初学者
2,876 次查看

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

0 项奖励
Steven_L_Intel1
2,876 次查看

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.

0 项奖励
Steven_L_Intel1
2,876 次查看

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

0 项奖励
回复