通过引用传递参数到交换函数
我在解决练习测试时遇到了这个问题
考虑以下用引用传递语言编写的代码,例如 FORTRAN 和这些关于代码的
subroutine swap(ix,iy)
it = ix
ix = iy ! line L1
iy = it ! line L2
end
program main
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end program
语句 语句:
- 编译器将生成代码来分配临时无名单元格, 将其初始化为 13,并传递单元交换的地址
- 执行时,代码将在 L1 行生成运行时错误
- 执行时,代码将在 L2 行生成运行时错误 程序
- 将打印 13 和 8
- 程序将打印 13 和-2
以上陈述中哪一项是正确的。
我认为S1和S4。有人可以确认一下吗? TIA
I encountered this problem while solving a practice test
Consider the following code written in a pass-by-reference language like
FORTRAN and these statements about the code
subroutine swap(ix,iy)
it = ix
ix = iy ! line L1
iy = it ! line L2
end
program main
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end program
Statements:
- The compiler will generate code to allocate a temporary nameless cell,
initialize it to 13, and pass the address of the cell swap - On execution the code will generate a runtime error on line L1
- On execution the code will generate a runtime error on line L2
- The program will print 13 and 8
- The program will print 13 and -2
Which of the above the above statement(s) is/are correct.
I think S1 and S4. Could anyone please confirm? TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,S1和S4是正确的。
另外,
S0a:该文件无法按原样编译,因为 L1:和 L2:是语法错误。 :)
S0b: 严格来说,将 FORTRAN 说成按引用传递语言是不正确的:
语言的名称是 Fortran
按引用传递是一种标准中未指定的实施细节。拷入、拷出是另一种可能的实现方式。此外,一些较新的 Fortran 语言功能 (F90+) 实际上意味着既不是按引用传递也不是复制输入复制输出的实现。请参阅指针作为虚拟参数和http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument/
Yes, S1 and S4 are correct.
Also,
S0a: The file is not compilable as is because the L1: and L2: are syntax errors. :)
S0b: Strictly speaking, speaking of FORTRAN as a pass-by-reference language is incorrect:
The name of the language is Fortran
Pass by reference is an implementation detail that is not specified in the standard. Copy-in, copy-out is another possible implementation. Also, some newer Fortran language features (F90+) in practice imply an implementation which is neither pass-by-reference nor copy-in-copy-out. See e.g. pointer as a dummy argument and http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument/
扩展@janneb的答案...这是该程序的 Fortran 95 语法正确版本:
由于这是非法的,因此无法保证输出。使用 gfortran 和 lax 编译器选项,我得到“S4”,即 13 和 8 的输出。
现在将子例程中的参数声明更改为:
并且 gfortran 拒绝编译它:
错误:变量定义上下文中的非变量表达式( INTENT = OUT/INOUT 的实际参数) at (1)
与 Intel ifort 类似:
test_swap.f90(31): error #6638:实际参数是表达式或常量;这是无效的,因为关联的虚拟参数具有显式 INTENT(OUT) 或 INTENT(INOUT) 属性。
看涨互换(ia、ib+5)
-----------------^
test_swap.f90 的编译中止(代码 1)
因此,对于正确编写的代码,现代 Fortran 编译器不允许这样做。如果你粗心的话,你可能会犯这个错误,而在 FORTRAN 77 及更早版本中,这个错误要容易得多。
Extending the answer of @janneb... Here is a Fortran 95 syntax-correct version of the program:
Since this is illegal, the output is not guaranteed. With gfortran and lax compiler options I get "S4", i.e., output of 13 and 8.
Now change the declaration of the arguments in the subroutine to:
and gfortran refuses to compile it:
Error: Non-variable expression in variable definition context (actual argument to INTENT = OUT/INOUT) at (1)
Similarly for Intel ifort:
test_swap.f90(31): error #6638: An actual argument is an expression or constant; this is not valid since the associated dummy argument has the explicit INTENT(OUT) or INTENT(INOUT) attribute.
call swap (ia, ib+5)
-----------------^
compilation aborted for test_swap.f90 (code 1)
So with properly written code, modern Fortran compilers do not allow this. You can make this mistake if you are sloppy, and it was much easier in FORTRAN 77 and earlier.