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

Cautionary tail about lbound and ubound on 64-bit program

jimdempseyatthecove
Honored Contributor III
511 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
Honored Contributor III
499 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.

0 Kudos
jimdempseyatthecove
Honored Contributor III
494 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

0 Kudos
Steve_Lionel
Honored Contributor III
471 Views

That would be fine.

0 Kudos
Reply