MAXLOC throws unnecessary exception in debug builds

MAXLOC throws unnecessary exception in debug builds

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 / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

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

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

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

Steve

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

You can accomplish what you want this way:

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

Steve

Faça login para deixar um comentário.