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

array boundary check in ifort

woshiwuxin
Novice
3,818 Views
Hi, everyone! The following code is completely correct in terms of syntax. But line 4 exceeds the boundary of 'a'. Normally, the compiler can not detect this problem, and this might produce some unexpected run time errors. Is there a compiler option to detect this error in ifort? Or, the programmer has to check manually. Thanks!
[fortran]subroutine modify(a) implicit none real(kind=8) :: a(*) a(5) = 10.5D0 return end subroutine modify program ex implicit none real(kind=8), allocatable :: a(:), b(:) allocate(a(3), b(3)) a(1) = 10.1D0 a(2) = 10.2D0 a(3) = 10.3D0 b(1) = 20.1D0 b(2) = 20.2D0 b(3) = 20.3D0 call modify(a) write(*,"(3F10.1)") a(1), a(2), a(3) write(*,"(3F10.1)") b(1), b(2), b(3) deallocate(a, b) stop end[/fortran]
0 Kudos
1 Solution
Steven_L_Intel1
Employee
3,818 Views
Sorry - missed that you declared the array as (*) in the subroutine. This bypasses all bounds checking. If you make it a deferred-shape array and have an explicit interface, you'll get the checking. Like this:

[fortran]program ex implicit none real(kind=8), allocatable :: a(:), b(:) allocate(a(3), b(3)) a(1) = 10.1D0 a(2) = 10.2D0 a(3) = 10.3D0 b(1) = 20.1D0 b(2) = 20.2D0 b(3) = 20.3D0 call modify(a) write(*,"(3F10.1)") a(1), a(2), a(3) write(*,"(3F10.1)") b(1), b(2), b(3) deallocate(a, b) stop contains subroutine modify(a) implicit none real(kind=8) :: a(:) a(5) = 10.5D0 return end subroutine modify end[/fortran]

View solution in original post

0 Kudos
4 Replies
woshiwuxin
Novice
3,818 Views
The code in line 21 is "deallocate(a, b)". I copyed and pasted the code, but the display always prefix a 'd' in front of "deallocate(a, b)".
0 Kudos
Steven_L_Intel1
Employee
3,818 Views
-CB or
-check bounds
0 Kudos
woshiwuxin
Novice
3,818 Views
I used -CB and -check bounds, but none gives a warning or error.
Without '-O0' the variable would be removed due to the code optimization.
xwu@xth3:~/tempcode> ifort --version
ifort (IFORT) 12.1.3 20120212
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.
xwu@xth3:~/tempcode> ifort -check bounds -O0 ex.f90 && ./a.out
10.1 10.2 10.3
10.5 20.2 20.3
xwu@xth3:~/tempcode> ifort -CB -O0 ex.f90 && ./a.out
10.1 10.2 10.3
10.5 20.2 20.3
0 Kudos
Steven_L_Intel1
Employee
3,819 Views
Sorry - missed that you declared the array as (*) in the subroutine. This bypasses all bounds checking. If you make it a deferred-shape array and have an explicit interface, you'll get the checking. Like this:

[fortran]program ex implicit none real(kind=8), allocatable :: a(:), b(:) allocate(a(3), b(3)) a(1) = 10.1D0 a(2) = 10.2D0 a(3) = 10.3D0 b(1) = 20.1D0 b(2) = 20.2D0 b(3) = 20.3D0 call modify(a) write(*,"(3F10.1)") a(1), a(2), a(3) write(*,"(3F10.1)") b(1), b(2), b(3) deallocate(a, b) stop contains subroutine modify(a) implicit none real(kind=8) :: a(:) a(5) = 10.5D0 return end subroutine modify end[/fortran]
0 Kudos
Reply