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

Intel Fortran does not catch this programming error

haifeng2009
Beginner
1,505 Views
Hello,

I found a situation which is most likely illegal in Fortran, but the Intel compiler failed to catch that. The situation is reproduced in the following simple code. The problem is in declaring A in subroutine test.The local variable K is used in declaring A, which I think is illegal in Fortran. I was hoping that the compiler can find this error for me, but Intel Fortran (10.1.013for windows and 10.1.?for Linux) did not. The code was "compiled" without trouble. I am wondering whether this is a bug of the compiler.

Haifeng

------------------------------------------------------------------------------------------------------
[cpp]

program main use test_mod real,allocatable :: A(:,:,:) allocate(A(n1,n2,n3)) call test(A) end program main module test_mod integer :: n1=2 integer :: n2=4 integer :: n3=6 contains subroutine test(A) real,intent(out) :: A(1-K(1,1):n1+K(2,1), & 1-K(2,1):n2+K(2,2), & n3) ! - local variables integer :: K(3,2) A = -1 end subroutine test end module test_mod[/cpp]
0 Kudos
10 Replies
TimP
Honored Contributor III
1,505 Views
So it appears:
$ gfortran -c test_mod.f90
test_mod.f90:7.35:

real,intent(out) :: A(1-K(1,1):n1+K(2,1), &
1
Error: Expression at (1) must be scalar
test_mod.f90:7.35:

real,intent(out) :: A(1-K(1,1):n1+K(2,1), &
1
Error: Expression at (1) must be scalar
0 Kudos
haifeng2009
Beginner
1,505 Views
This is the output from PGI fortran

pgf90 -c test_mod.f90

PGF90-S-0043-Illegal attempt to redefine symbol k (test_mod.f90: 12)
0 inform, 0 warnings, 1 severes, 0 fatal for test
0 Kudos
Steven_L_Intel1
Employee
1,505 Views
Interesting - thanks. I'll report this to the developers. DPD200141301

I will note that Intel Fortran requires that the module appear first in the source.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,505 Views

You are never minding the fact that

! - local variables
integer :: K(3,2)

the contents of K is(are) undefined (hold junk data) at the time of

subroutine test(A)
real,intent(out) :: A(1-K(1,1):n1+K(2,1), &
1-K(2,1):n2+K(2,2), &
n3)

Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
1,505 Views
Jim, this is an error. The request is for the compiler to complain. I don't think anyone is suggesting that this coding be accepted and do something useful.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,505 Views

I would have to say the particular error message was in error, however the coding was in error as well. Was there a bug or was the compiler confused? (And issued the closest error message to the problem encountered.)

Had K been located inside the module data (above contains) then it would (should have been) correct programming. As to if the compiler emits the same error message in erroris up to someone to run a test. (Note, K not having been initialized would have been a seperate but run-time error).

Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
1,505 Views

ifort did not give any error at all, which it should have. I have no clue what kind of code it generated for this.

Steve
0 Kudos
haifeng2009
Beginner
1,505 Views

A few things to add

This is an error in a big Fortran code thatwe didn't find, and neither did the Intel compiler. Fortunately it was detected by another compiler (PGI) recently. The correct coding should be declaring A as A(n1,n2,n3) without those K.

There is no run-time error with run-time checking of array bounds and uninitialized variables.

When I step into this subroutine during debugging in Visual studio, the actual value of K used in declaring A happened to be zero. I don't know why it is zero rather than junk data. The code worked correctly for a long time. (If K were not zero in our code, I am sure the results from the codewon't be correct at all for us)

Thanks,
Haifeng
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,505 Views

>>(If K were not zero in our code, I am sure the results from the codewon't be correct at all for us)

The values within K(3,2) are subscript biases for how you intend to index A within the subroutine. Your subroutine declared the valid index ranges for A as seen inside the subroutine (not the indexes used outside the subroutine)

A = -1

Would not have generated a subscript out of bounds error.

*** However, when values of K(3,2) are not zero then -1's may have been written over the top of other data.

As to if this would affect the results of your computations it may or may not have affected the results.

Jim Dempsey

0 Kudos
haifeng2009
Beginner
1,505 Views

>>(If K were not zero in our code, I am sure the results from the codewon't be correct at all for us)

The values within K(3,2) are subscript biases for how you intend to index A within the subroutine. Your subroutine declared the valid index ranges for A as seen inside the subroutine (not the indexes used outside the subroutine)

A = -1

Would not have generated a subscript out of bounds error.

*** However, when values of K(3,2) are not zero then -1's may have been written over the top of other data.

As to if this would affect the results of your computations it may or may not have affected the results.

Jim Dempsey


Thanks for the reply.

It isimpossible that our code can work correctly if K/=0. The association of A(1:n1,1:n2,1:n3) fromthe calling subroutine with the dumy argument A(1-K(1,1):n1+K(2,1),1-K(1,2):n2+K(2,2),1:n3)) leads to two consequences inside the subroutine: 1. changing the lower subscripts of the array and 2. changing the shape of the array. So A(i,j,k) inside the subroutine cann'tbe the right "A(i,j,k)" in the calling subroutine givenany non-zero elements in K(1:2,1:2).

Haifeng
0 Kudos
Reply