- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
[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]
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
-CB or
-check bounds
-check bounds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
[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]

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page