admin管理员组

文章数量:1122832

The following code is working as expected, unless I'm using -fmax-stack-var-size=0 with gfortran 12.3, 13.3 or 14.2.1. In which case, the function returns an empty string.

program test_clean_str
    implicit none
    character(len=100) :: input_str
    character(len=:), allocatable :: result_str

    input_str = '  Test String  '

    result_str = clean_str(input_str)

    print *, 'Result:-', result_str,"-"

contains

    function clean_str(str_in) result(str_out)
        implicit none
        character(len=*), intent(in) :: str_in
        character(len=:), allocatable :: str_out

        str_out = trim(str_in)
        print*,"-",str_out,"-"

    end function clean_str

end program test_clean_str

This change of behavior does not occur if the function is rewritten as a subroutine or if I'm working with integer arrays instead of character string.

Does this change of behavior is to be expected ?

Edit: remove the mention to ifort and ifx as they are not concerned, and add precision about the concerned version of gfortran

本文标签: fortranFunction wrongly returning empty character string if allocated on the heapStack Overflow