https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103115

            Bug ID: 103115
           Summary: reallocation of character array fails when appending a
                    constant size 4 array
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ygalklein at gmail dot com
  Target Milestone: ---

The following example program:
```fortran
program reallocationCharacterArray
    implicit none
    character(:), dimension(:), allocatable :: titles
    titles = ["1"]
    titles = [titles,&
            "2",&
            "3",&
            "4",&
            "5"&
            ]
    block
        integer :: iTitle
        do iTitle = 1, size(titles)
            write(*, "('titles(',i2,') = ',a)") iTitle, trim(titles(iTitle))
        end do
    end block
end program reallocationCharacterArray
```

when compiled using gfortran 11.1 or 10.3 results with the following output:
`
size(titles) = 5
titles( 1) = 1
titles( 2) = 
titles( 3) = 
titles( 4) = 
titles( 5) = 
`

which is clearly wrong.
One can see that the reallocation succeded in increasing the size of titles
from 1 to 5 - but the assignment to all the places from 2 to 5 are erronous.

one should note that using intel compiler 2021.4 - results with the following
output:
`
size(titles) = 5
titles( 1) = 1
titles( 2) = 2
titles( 3) = 3
titles( 4) = 4
titles( 5) = 5
`

which is the right output.

One should also note that commenting one line from the constant array being
appended - i.e appending an array of size 3 (instead of size 4) results with
the right output when using gfortran, i.e

the following program (note the ! sign commenting one line):
```fortran
program reallocationCharacterArray
    implicit none
    character(:), dimension(:), allocatable :: titles
    titles = ["1"]
    titles = [titles,&
            ! "2",&
            "3",&
            "4",&
            "5"&
            ]
    write(*, "('size(titles) = ',i1)") size(titles)
    block
        integer :: iTitle    
        do iTitle = 1, size(titles)
            write(*, "('titles(',i2,') = ',a)") iTitle, trim(titles(iTitle))
        end do
    end block
end program reallocationCharacterArray
```

results with the following right output:
`
size(titles) = 4
titles( 1) = 1
titles( 2) = 3
titles( 3) = 4
titles( 4) = 5
`

both in gfortran and ifort.

Reply via email to