tags:

views:

150

answers:

3

I have the following FORTRAN

  SUBROUTINE SETPATHS(INPUT)
  !DEC$ ATTRIBUTES DLLEXPORT::SetPaths

  CHARACTER*20 INPUT
  CHARACTER*20 DIRECTORY

  DIRECTORY = 'ABCDEFG'

  WRITE(6, *) 'INPUT LEN_TRIM = ', LEN_TRIM(INPUT)
  WRITE(6, *) 'DIRECTORYLEN_TRIM = ', LEN_TRIM(DIRECTORY)

  END SUBROUTINE

And I'm calling the function from C#, passing in 'ABCDEFG'.

When I set a breakpoint on my debugger, INPUT and DIRECTORY have the exact same characters. Both have 'ABCDEFG' followed by the same number of trailing spaces.

However, the program outputs

  INPUT LEN_TRIM = 20
  DIRECTORYLEN_TRIM = 7

Is this correct behavior? If the two strings have the same values, why does LEN_TRIM give different results?

Update: I found this documented problem (although it's not my Intel 8.1 compiler). http://support.microsoft.com/kb/89131

+1  A: 
sbo@dhcp-045:~ $ ./a.out 
 INPUT LEN_TRIM =            7
 DIRECTORYLEN_TRIM =            7
sbo@dhcp-045:~ $ more test.f90 
SUBROUTINE SETPATHS(INPUT)

CHARACTER*20 INPUT
CHARACTER*20 DIRECTORY

DIRECTORY = 'ABCDEFG'

WRITE(6, *) 'INPUT LEN_TRIM = ', LEN_TRIM(INPUT)
WRITE(6, *) 'DIRECTORYLEN_TRIM = ', LEN_TRIM(DIRECTORY)

END SUBROUTINE
program test
    character*20 foo
    foo = "ABCDEFG"
    call setpaths(foo)
end program

What compiler are you using ? here gfortran.

Tried with ifort as well

$ ifort -v
Version 8.0
$ ifort test.f90
$ ./a.out 
 INPUT LEN_TRIM =            7
 DIRECTORYLEN_TRIM =            7

I don't know how the C# interface can introduce problems, but the semantics of LEN_TRIM are quite easy... if you say that the strings appear as equal in the debug, there's something very fishy going on.

Stefano Borini
I'm using the Intel compiler.
Larsenal
I don't know if I have access to ifc, I'll try and see... maybe.... What version ?
Stefano Borini
I'm using version 8.1. I'm wondering if it has more to do with the fact that I passed in the StringBuilder from C#.
Larsenal
That, or a bug introduced in the ifort general library, but I doubt it.
Stefano Borini
+1  A: 

it may be looking for Terminator character, char(0). it could be different from \n character

other possibility the string length was not passed correctly. fortran 77 string is passed as two values, string pointer and string length. there is no standard how string length is passed, most compilers stick it as a hidden parameter in the end. could be 90 does the same thing for compatibility.

aaa
The typical Fortran string, which the OP is using, is fixed length, padded on the end with blanks. The length is the declared length, not the used length. There is no terminating character.
M. S. B.
+1  A: 

By explicitly padding my C# StringBuilder with trailing spaces, the LEN_TRIM behaved as expected. This Microsoft KB seems to be related.

It is strange, however, that in the debugger the trailing spaces appeared even before I did the explicit padding.

Larsenal
In your previous question, http://stackoverflow.com/questions/2018277/how-to-pass-parameter-from-c-to-fortran-with-parameter-of-type-character50, I explained how Fortran strings work and suggested that you needed to fill the remainder of the strings with blanks.
M. S. B.
Yes, I read that. However, when I looked at them in the debugger they were already blanks without explicitly filling them. It wasn't a bunch of garbage. All character 20's.
Larsenal