Arrays of characters passed to function

Hi everyone,

I’d like to understand what happens when passing an implicit array of characters (I don’t know if it is the proper name) to a function. In the following code for instance, the first call to func1 behaves as expected and prints all elements of test_array.

The second call, however, results in a core dumped with SIGABRT, and prints the following:

ARRAY = my_directory/file1my_directory/file2my_directory/dum1 my_directory/dum2

I compiled using Gfortran 13.2.1 on Fedora, with gfortran -o test program.f90.

Is it something related to the compiler, or is the code wrong ?

Thanks a lot for your insight !

program test
  implicit none

  character(len=256) :: test_array(4)
  character(len=:), allocatable :: prefix
  integer :: res

  prefix = 'my_directory'
  test_array = [ character(len=256) :: &
       & trim(prefix)//'/file1', &
       & trim(prefix)//'/file2', &
       & trim(prefix)//'/dum1', &
       & trim(prefix)//'/dum2' &
       & ]

  print *, 'Test with "res = func1(test_array)"'
  res = func1(test_array)
  print *, 'Test with "res = func1([ character(len=) :: ...] )"'
  res = func1([ character(len=256) :: &
       & trim(prefix)//'/file1', &
       & trim(prefix)//'/file2', &
       & trim(prefix)//'/dum1', &
       & trim(prefix)//'/dum2' &
       & ])

  function func1(array) result(res)
    character(len=*), intent(in) :: array(:)

    integer :: res

    print *, 'ARRAY = ', array
    res = 0
  end function func1
end program test

Welcome to the forum!

I ran your program with gfortran version 12.2.0 and Intel oneAPI Fortran, ifort version 2021.9.0 and ifx version 2023.1.0: the first version fails as you indicated, but the versions built with Intel work as expected. I see no problem with the source code, so I definitely think that this is a bug in gfortran. You may want to report it.

Thanks for your answer !

I reported the bug to Gfortran.

Also, I noticed that both function calls run fine when removing the trim(prefix)// part of the individual strings.

1 Like

What happens if you use “prefix(1:length) // …” instead? (with length = len_trim(prefix))

I get the same error.

Hm, perhaps it is not so much the use of trim() but the use of concatenation. Could be useful as a further analysis of the bug.

The code compiles and runs with gfortran 13.1.0, but the output looks odd. I changed the write statement to

print *, 'len=', len(array), ' ARRAY = ', array

The first call prints the value of 256, while the second call prints the value of 18. The subsequent printed strings reflect those two len values.

With nagfor, both calls print the value of 256, which to my eye looks correct.

If I replace all of the occurrences of trim(prefix) with simply prefix, then both compilers print the same output as they did before. I don’t see any reason why the function argument should be trimmed or the len() value changed. Am I missing something here, or is this a gfortran bug?

Apparently it is a bug in gfortran, that was reported some time ago (the bug report I posted was labelled as a duplicate of this one) :