A suggested solution for returning a variable length string in Fortran was from this question:
  function itoa(i) result(res)
    character(:),allocatable :: res
    integer,intent(in) :: i
    character(range(i)+2) :: tmp
    write(tmp,'(i0)') i
    res = trim(tmp)
  end function
Am I right in my understanding, that the result of this function is never deallocated? So for large numbers and a lot of calls you could run into memory leaks.
So what I mean exactly is the case where I don't assign the result of my function, but use it "In place" as in
do i = 1, n
    write(*, *) "tmp_"//itoa(i)
end do
I clearly have no reference to the result on which I could call deallocate and while I am looping it definitely doesn't go out of scope.
If I understand you (@Francescalus) correctly, I can still rely on the fact that it is deallocated.