Findloc with allocatable character arrays gfortran

I have a program where I am using allocatable character arrays to read in and write out headers for files. Within this program, I also want to be able to search through the array for a specific header, and so I am using the findloc intrinsic function to do this. Here is a simple example program:

program main
    implicit none

    character(len=:),dimension(:),allocatable :: char_array

    allocate(character(len=100) :: char_array(3))
    char_array = ["hello", "world", "array"]

    print*, findloc(char_array,"hello")
    print*, findloc(char_array,"world")
    print*, findloc(char_array,"array")

end program main

When I compile and run this program with gfortran 9 (linux), everything works as expected and the correct indices (1,2, and 3) are printed to the screen. Compiled with gfortran 11 and 12, however, I get a segmentation fault for an invalid memory reference, and the backtrace points directly to the first line with findloc (line 9). It might be worth noting that if the array is not allocatable and is instead declared directly with its length and size, the program runs as expected with all previously mentioned versions of gfortran.

So my question is: is this a valid program and is this a bug with gfortran’s implementation of findloc or is this invalid and I was just lucky that it works in gfortran 9? It might also be interesting to test this program with ifort or ifx to see if they differ from this behaviour.

Maybe this is on purpose, but the allocate() statement in that program is redundant. The very next assignment statement reallocates the array to the new len=5 and size=3 values.

You can add the line

print*, len(char_array), size(char_array)

to verify the reallocation. Your code works on MacOS with GNU Fortran (Homebrew GCC 13.2.0) 13.2.0 and with NAG Fortran Compiler Release 7.1(Hanzomon) Build 7114.

You are right about the redundant allocation, however, even without the allocation statement, I still get a segmentation fault as before.

Problem still there with gfortran 13. I think you should report the bug @jaiken. All 3 of AMD flang, ifort and ifx printed 1 2 3 on successive lines. By the way, the allocate statement is unnecessary in the program because char_array was automatically reallocated (with len=5) by the next statement
char_array = ["hello", "world", "array"]
All 4 of the compilers I tried gave the same results (one good, three not) with and without the allocate statement.

As @Ronshepard sent his reply while I was testing @jaiken’s program and drafting my reply, and he found the code OK with GNU Fortran 13.2, while I confirmed the bug with what I had said was version 13, I had to check. My GNU Fortran is a slightly earlier version than @Ronshepard’s:
gcc version 13.1.0 (Ubuntu 13.1.0-8ubuntu1~22.04)
It seems that the Gfortran people fixed the bug between versions 13.1 and 13.2.

Thank you both @Harper and @RonShepard; I think that it is pretty obvious now that the segmentation fault is just a bug that was present for a couple versions of gfortran. I will just have to be careful about which version I compile with, but that isn’t a big problem.

With GNU Fortran (GCC) 14.0.1 20240121 on Windows from Equation.com the program gives the expected output.

Just for anyone who is interested or anyone who runs into this issue in the future, I found this bug report about the findloc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110288

The bug was present for gfortran version 11, 12, and 13. Fixes were implemented for gfortran-14 and backported for version 11, 12, and 13 on July 14th 2023, however, only gfortran-13 has had a new release (13.2) since and so the bug is still present in the current releases of gfortran-11 (11.4) and gfortran-12 (12.3). From what I understand, the next releases of gfortran-11 and gfortran-12 should have the bug fixed as well.

Thanks again to everyone who tested out my test program.

1 Like