MAXLOC throws unnecessary exception in debug builds

MAXLOC throws unnecessary exception in debug builds

Tobias Loew's picture

Hi

the following code throws an exception in debug-builds as it checks if "arr(0)" is a valid address even though it will never be accessed. This is a bit annoying as in release builds the code runs correct.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    program Console4

    implicit none
    real*8 arr(3)
    integer*4 i(1)
    integer*4 arri(3)
    ! Variables

        arri(1)=1
        arri(2)=2
        arri(3)=0
        
        arr(1)=4.0
        arr(2)=5.0
        arr(3)=6.0

        i=maxloc(arr(arri(:)), arri(:).GT.0)
        
        print *,i
    end program Console4

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

regards

Tobias

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Steve Lionel (Intel)'s picture

I'm not convinced that the compiler is wrong here. The mask isn't applied until after the first argument to MAXLOC is evaluated, and it's here that you're getting the array bounds error.  The standard doesn't suggest that the evaluation of the ARRAY argument is itself masked by MASK. I also tried gfortran 4.7 and it gave a similar error. You don't get the error in Release because bounds checking is off by default there.

Steve
Tobias Loew's picture

Hi Steve,

thanks for the comment. I'll expand the array to arr(0:3) to avoid the exception. IMHO there should be a statement in the documentation w.r.t. this topic.

regards

Tobias

Steve Lionel (Intel)'s picture

What kind of statement would you like? How is this different from any other array expression argument?

Steve
Tobias Loew's picture

I meas a statement like you posted:

"The mask isn't applied until after the first argument to MAXLOC is evaluated, "

then it would be clear that constructs like mine lead to exceptions/undefined behaviour. In the documentation to "MAXLOC" I could not find such a statement.

regards

Tobias

Steve Lionel (Intel)'s picture

You can accomplish what you want this way:

i = maxloc(arr(pack(arri, arri>0)))

Steve

Login to leave a comment.