FORTRAN 的 LEN_TRIM 对传递的参数的行为是否有所不同?

发布于 2024-08-16 17:51:44 字数 730 浏览 3 评论 0原文

我有以下 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

我正在从 C# 调用该函数,并传入“ABCDEFG”。

当我在调试器上设置断点时,INPUT 和 DIRECTORY 具有完全相同的字符。两者都有“ABCDEFG”,后跟相同数量的尾随空格。

但是,程序输出

  INPUT LEN_TRIM = 20
  DIRECTORYLEN_TRIM = 7

这是正确的行为吗?如果两个字符串具有相同的值,为什么 LEN_TRIM 给出不同的结果?

更新:我发现了这个已记录的问题(尽管它不是我的 Intel 8.1 编译器)。 http://support.microsoft.com/kb/89131

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花浅忆 2024-08-23 17:51:44
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

你使用什么编译器?这里是gfortran。

也尝试过 ifort,

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

我不知道 C# 接口如何引入问题,但 LEN_TRIM 的语义非常简单...如果你说字符串在调试中显示为相等,那么就会出现一些非常可疑的情况。

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.

愿与i 2024-08-23 17:51:44

它可能正在寻找终结符字符 char(0)。它可能与 \n 字符不同,

其他可能性是字符串长度未正确传递。 Fortran 77 字符串作为两个值传递:字符串指针和字符串长度。没有标准如何传递字符串长度,大多数编译器最后将其作为隐藏参数。为了兼容性,90 可能会做同样的事情。

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.

墨离汐 2024-08-23 17:51:44

通过使用尾随空格显式填充 C# StringBuilder,LEN_TRIM 的行为符合预期。 此 Microsoft 知识库似乎是相关的。

然而,奇怪的是,在调试器中,甚至在我进行显式填充之前就出现了尾随空格。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文