Topic: Small problem debugging character variables

The debugger doesn't properly list the local variables in subroutine greet in the following example.

subroutine greet(name)
   character(len=*) :: name
   print *,'Hello ',trim(name)
end subroutine greet

program main
   character(len=10) :: name
   name = 'Michael'
   call greet(name)
end program main
--
David

Re: Small problem debugging character variables

davidb wrote:

The debugger doesn't properly list the local variables in subroutine greet in the following example.

David,

I'm not sure what you're seeing that you'd classify as incorrect.  When I debug, I enter the greet  subroutine, and I see two local variables, _name and name.  The former is an artifact of using assumed-length character strings, and it shows a correct value of 10.  The latter is the character string  itself, and it appears to be correct as well.  What seemed to be the issue in your case?

Jeff Armstrong
Approximatrix, LLC

Re: Small problem debugging character variables

This is not a big problem. I can see that _name is the length of the variable. However, this is not consistent with the way other variables are listed. It seems to happen only with character dummy arguments. If I add a new local character(len=10) variable to the example it is listed without an additional "length" variable:

subroutine greet(name)
   character(len=*) :: name
   character(len=10) :: nice
   nice = ', good day'
   print *,'Hello ',trim(name), nice
end subroutine greet

program main
   character(len=10) :: name
   name = 'Michael'
   call greet(name)
end program main
--
David

Re: Small problem debugging character variables

David,

The presence of _name is somewhat of a legacy of Fortran compilers.  Unlike your local character variable, the name variable was an argument.  Historically, and GNU Fortran might do this for compatibility reasons, the length of a character of assumed length always followed the character itself.  I don't believe GNU's old g77 compiler would do this same procedure with arrays, though, which is why you wouldn't see something similar.

Clearly your nice local variable also has a length stored somehow and somewhere, but if you were to pass it to another subroutine as an argument, there would be a corresponding _nice since it would be necessary for compatibility.

Jeff Armstrong
Approximatrix, LLC

Re: Small problem debugging character variables

Thanks Jeff.

In order to pass character variables, a hidden argument containing the length is also passed. This has the same name as the variable prefixed with _.

However, users are not allowed to declare variables prefixed with _ so one option open to you would be to suppress such variables from the list.

So this is just a suggestion and not a bug at all. smile

--
David