开发者

Does FORTRAN's LEN_TRIM behave differently with passed parameters?

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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜