Why no bound check within if statement?

Why no bound check within if statement?


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


if (stLocalHazard(iZipIndex) .gt. 0) then





5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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. 

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


D:\Projects>ifort /check:bounds t.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 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.


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


Steve (aka "Doctor Fortran") - Retired from Intel

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

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.

Steve (aka "Doctor Fortran") - Retired from Intel

Leave a Comment

Please sign in to add a comment. Not a member? Join today