是什么FORTRAN意图(IN,OUT,INOUT)之间的明确区别? [英] What is the explicit difference between the fortran intents (in,out,inout)?

查看:1981
本文介绍了是什么FORTRAN意图(IN,OUT,INOUT)之间的明确区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了一会儿书籍,这里计算器和一般的网页后,我发现,这是很难找到一个简单的解释了FORTRAN的说法意图之间的真正差别。我已经明白它的方式,是这样的:

After searching for a while in books, here on stackoverflow and on the general web, I have found that it is difficult to find a straightforward explanation to the real differences between the fortran argument intents. The way I have understood it, is this:


  • 意图(中) - 实际参数在入境复制到虚拟参数

  • 意图(出) - 假人参数指向实际参数(它们都指向内存中的同一个地方)

  • 意图(INOUT) - 虚数在本地创建,然后复制到当过程结束后实际参数

  • intent(in) -- The actual argument is copied to the dummy argument at entry.
  • intent(out) -- The dummy argument points to the actual argument (they both point to the same place in memory).
  • intent(inout) -- the dummy argument is created locally, and then copied to the actual argument when the procedure is finished.

如果我的理解是正确的,那么我也想知道为什么人会希望使用意图(出),因为意图(INOUT )需要更少的工作(无数据复制)。

If my understanding is correct, then I also want to know why one ever wants to use intent(out), since the intent(inout) requires less work (no copying of data).

推荐答案

意图只是提示编译器,你可以扔掉这些信息路程,违反它。意图存在几乎完全以确保你只做你计划在一个子程序做什么。编译器可能会选择信任你和优化的东西。

Intents are just hints for the compiler, and you can throw that information away and violate it. Intents exists almost entirely to make sure that you only do what you planned to do in a subroutine. A compiler might choose to trust you and optimize something.

这意味着意图(中)不是按值传递。您仍然可以覆盖原来的值。

This means that intent(in) is not pass by value. You can still overwrite the original value.

program xxxx  
    integer i  
    i = 9  
    call sub(i)  
    print*,i ! will print 7 on all compilers I checked  
end  
subroutine sub(i)  
    integer,intent(in) :: i  
    call sub2(i)  
end  
subroutine sub2(i)  
    implicit none  
    integer i  
    i = 7  ! This works since the "intent" information was lost.  
end

program xxxx  
    integer i  
    i = 9  
    call sub(i)  
end  
subroutine sub(i)  
    integer,intent(out) :: i  
    call sub2(i)  
end  
subroutine sub2(i)  
    implicit none   
    integer i  
    print*,i ! will print 9 on all compilers I checked, even though intent was "out" above.  
end  

这篇关于是什么FORTRAN意图(IN,OUT,INOUT)之间的明确区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆