Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.
27778 Discussions

Cautionary tail about lbound and ubound on 64-bit program

jimdempseyatthecove
Black Belt
244 Views

Consider the following 64-bit program:

program foo
    implicit none
    real, allocatable :: array(:)
    integer(8) :: i
    
    i = Z'0000000100000000' + 5_8
    allocate(array(i)) ! 2^32 + 5
    print *,ubound(array), ubound(array,1,kind(i))
end program foo
 Output:
           5            4294967301

It is easy (for me) to overlook that the IVF language states:

The result type is integer. If kind is present, the kind parameter of the result is that specified by kind otherwise, the kind parameter of the result is that of default integer.

Therefore, in 64-bit programs care must be taken with arrays who's bounds exceed those of default integer (integer(4)).

 

This can introduce a programming error "Sight seen but unseen"

 

Jim Dempsey

3 Replies
Steve_Lionel
Black Belt Retired Employee
232 Views

This applies to many intrinsics, and is exactly why the KIND argument was added to many of them.

I'll also comment that the assignment to i is non-standard, and the results implementation-dependent. Even in Fortran 202x, which expands the use of BOZ constants, this would not be allowed. See Doctor Fortran in "We're All BOZos on This Bus" - Doctor Fortran (stevelionel.com) for more information.

jimdempseyatthecove
Black Belt
227 Views

>>I'll also comment that the assignment to i is non-standard

I found that out

The compiler balked at:

    i = Z'0000000100000000' + 5 ! sans _8

 

FWIW would this be preferred?

 

   i = INT(Z'0000000100000000', kind(i)) + 5

 

Jim Dempsey

Steve_Lionel
Black Belt Retired Employee
204 Views

That would be fine.

Reply