Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26746 Discussions

Why no bound check within if statement?

Song__James
Beginner
69 Views

 

I found that the compiler does NOT check the bound of an array within if statement. But if you use write statement, it would check.

That is strange.

real stLocalHazard (100)

do iZipIndex =1 , 200
c                                                   
if (stLocalHazard(iZipIndex) .gt. 0) then
 
endif
 
enddo
0 Kudos
4 Replies
andrew_4619
Honored Contributor I
69 Views

Are you expecting a compile time error for that? If so you are being a bit optimistic to expect the compiler to evaluate the possible run time values of a variable that is used as an array index. Your case is simple but the general case is not and in many cases it would be impossible for the compile to predict. You will get a run time error however.

Much better practice to have say:

do iZipIndex = 1 , size( stLocalHazard)

An then if you change the dimension the code is still good. 

Steve_Lionel
Black Belt Retired Employee
69 Views

How about you post a real, complete program that demonstrates your claim? I tried it and it did check the bounds.

D:\Projects>type t.f90
real stLocalHazard (100)

stLocalHazard = 0
do iZipIndex =1 , 200



if (stLocalHazard(iZipIndex) .gt. 0) then
print *, iZipIndex


endif



enddo
end
D:\Projects>ifort /check:bounds t.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 18.0.0.124 Build 20170811
Copyright (C) 1985-2017 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.11.25547.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:t.exe
-subsystem:console
t.obj

D:\Projects>t.exe
forrtl: severe (408): fort: (10): Subscript #1 of the array STLOCALHAZARD has value 101 which is greater than the upper bound of 100

Image              PC                Routine            Line        Source
t.exe              00007FF7CE52DB01  Unknown               Unknown  Unknown
t.exe              00007FF7CE521142  Unknown               Unknown  Unknown
t.exe              00007FF7CE56FEB2  Unknown               Unknown  Unknown
t.exe              00007FF7CE570255  Unknown               Unknown  Unknown
KERNEL32.DLL       00007FFDC0851FE4  Unknown               Unknown  Unknown
ntdll.dll          00007FFDC098EF91  Unknown               Unknown  Unknown

 

gib
New Contributor II
69 Views

It seems (as Andrew infers) that the OP wants the bounds error to be detected at compile time.

Steve_Lionel
Black Belt Retired Employee
69 Views

I don't think so, at least based on the comment about WRITE statements. A similar test in a WRITE would not give compile-time errors. ifort CAN give a compile-time bounds error, but it requires a constant for the subscript and that's not typical.

Rather, I think the OP saw something, jumped to a conclusion as to what the cause was, and then threw in an uncompilable snippet based on that assumption.

Reply