1

I am trying the following code, which simply adds two array constructors to get a one-dimensional array. However, depending on whether one is used in these expressions (outside implied DO loop), I get incorrect results with gfortran-5.3 for (1), (3), and (6). On the other hand, ifort-14.0 and Sun fortran 8.7 give correct results for all cases. Strangely, if I try a similar but more complicated expression, even Sun fortran starts to behave strangely (i.e., it gets frozen during compilation...) So I am wondering whether I am using some wrong or dangerous syntax in these expressions. Any idea...? (BTW, if I leave only one constructor between the two, they always work as expected.)

program main
    implicit none
    integer :: k
    integer, dimension(2), parameter :: zero = [0,0], one = [1,1]
    integer, parameter :: N = 1

    print *, [  one,   ( zero, k=1,N) ] + [ ( zero, k=1,N),  one   ]  !! (1)
    print *, [ [1, 1], ( zero, k=1,N) ] + [ ( zero, k=1,N), [1, 1] ]  !! (2)
    print *, [  one,   ([0, 0],k=1,N) ] + [ ([0, 0],k=1,N),  one   ]  !! (3)
    print *, [ [1, 1], ([0, 0],k=1,N) ] + [ ([0, 0],k=1,N), [1, 1] ]  !! (4)
    print *, [  1, 1,  ( 0, 0, k=1,N) ] + [ ( 0, 0, k=1,N),  1, 1  ]  !! (5)
    print *, [  one,   ( 0, 0, k=1,N) ] + [ ( 0, 0, k=1,N),  one   ]  !! (6)

    print *, [ (one, k=1,1), (zero, k=1,N) ] + [ (zero, k=1,N), (one, k=1,1) ] !! (7)
endprogram

Results with ifort14.0 and Sun Fortran 8.7:

       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1

Results with gfortran-5.3:

       1           1           0           1
       1           1           1           1
       1           1           0           1
       1           1           1           1
       1           1           1           1
       1           1           0           1
       1           1           1           1
2
  • Those look fine to me. Commented Apr 20, 2016 at 20:03
  • @francescalus OK, thanks very much! Commented Apr 20, 2016 at 20:13

1 Answer 1

1

The results are as expected when using the associate construct

    program main

        use, intrinsic :: iso_fortran_env, only: &
            compiler_version, &
            compiler_options

        implicit none
        integer :: k
        !integer, dimension(2), parameter :: zero = [0,0], one = [1,1]
        integer, parameter :: N = 1

        associate( zero => [0,0], one => [1,1] )
            print *, [  one,   ( zero, k=1,N) ] + [ ( zero, k=1,N),  one   ]  !! (1)
            print *, [ [1, 1], ( zero, k=1,N) ] + [ ( zero, k=1,N), [1, 1] ]  !! (2)
            print *, [  one,   ([0, 0],k=1,N) ] + [ ([0, 0],k=1,N),  one   ]  !! (3)
            print *, [ [1, 1], ([0, 0],k=1,N) ] + [ ([0, 0],k=1,N), [1, 1] ]  !! (4)
            print *, [  1, 1,  ( 0, 0, k=1,N) ] + [ ( 0, 0, k=1,N),  1, 1  ]  !! (5)
            print *, [  one,   ( 0, 0, k=1,N) ] + [ ( 0, 0, k=1,N),  one   ]  !! (6)
            print *, [ (one, k=1,1), (zero, k=1,N) ] + [ (zero, k=1,N), (one, k=1,1) ] !! (7)
        end associate

        print *, 'This result was compiled by ', &
            compiler_version(), ' using the options ', &
            compiler_options()

    end program

Using GNU Fortran (Debian 5.3.1-14) 5.3.1 20160409 I get

       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1
       1           1           1           1

       This result was compiled by GCC version 5.3.1 20160409 using the options -mtune=generic -march=x86-64 -O3 -Wall -std=f2008ts
Sign up to request clarification or add additional context in comments.

2 Comments

Removing the parameter attribute also yields the expected results.
I see... I have tried removing the "parameter", and it does give the expected result also on my computer (CentOS + gfort4.8 or 5). So the culprit seems the parameter attribute...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.