Incorrect code for a f95 character function

pgf95 compiles the following code:

program test

  character(8) :: foo = 'foo', bar = 'bar'

  print *, '"'//trimconcat(foo,bar)//'"'

contains

  function trimconcat(a, b)
    character(*), intent(in) :: a, b
    character(len_trim(trim(a)//b)) :: trimconcat

    trimconcat = trim(trim(a)//b)
  end function trimconcat

end program test

However, the following output is produced during execution:

0: ALLOCATE: 18446744072635804528 bytes requested; not enough memory

I expected “foobar” to be printed. Does this work in the most recent pgf95?

I am using pgf95 6.2-3 64-bit target on x86-64 Linux.

Hi rusteve,

Thank you for the report and example code. I have filed a technical problem report (TPR#4254) and have sent the issue to our compiler team for further review. The problem is caused by a compiler generated temporary variable to hold the result of “trim(a)” within “len_trim(trim(a)//b)”. Unfortunately, the only work around I see is to move the trim intrinsic from “trimconcat” to the caller.

For example:

% cat trim2.f90
program test
  character(8) :: foo = 'foo', bar = 'bar'
  print *, '"'//trimconcat(trim(foo),bar)//'"'
contains
  function trimconcat(a, b)
    character(*), intent(in) :: a, b
    character(len_trim(a//b)) :: trimconcat
    trimconcat = a//b
  end function trimconcat
end program test
% pgf90 trim2.f90
% a.out
 "foobar"

Best Regards,
Mat

Hi rusteve,

We found a better work around:

% cat trim3.f90
program test
  character(8) :: foo = 'foo', bar = 'bar'
  print *, '"'//trimconcat(foo,bar)//'"'
contains
  function trimconcat(a, b)
    character(*), intent(in) :: a, b
    character(len_trim(a)+len_trim(b)) :: trimconcat
    trimconcat = trim(trim(a)//b)
  end function trimconcat
end program test
% a.out
 "foobar"
  • Mat

I can confirm that this works. Thanks!