为什么Fortran中存在隐含的SAVE属性? [英] Why is there an implied SAVE attribute in Fortran?
问题描述
程序测试
隐式无
调用foo()
()
$子程序foo b写(*,*)i
结束子程序foo
结束程序测试
will print
1
2
由于这在许多其他语言中有所不同,我在想为什么Fortran标准委员会选择这种行为?
非常感谢!
Mike
这主要是由于历史原因。旧编译器(Fortran IV(66)及之前)实现了主要使用静态内存的程序。旧机器甚至没有任何堆叠。因此,程序的行为,因为变量定义为 save
。
变量初始化的前身 DATA
语句,更像是定义静态内存的初始内容(与汇编中数据段的指令类似),比从C中知道的调用变量初始化要多。该语法后来变得类似于C变体。
If I initialize a variable in a Fortran declaration statement, that variable implicitly receives a SAVE attribute and the initialization expression will only be executed once.
For example, the following program
program test
implicit none
call foo()
call foo()
contains
subroutine foo ()
integer :: i = 0
i = i + 1
write(*,*) i
end subroutine foo
end program test
will print
1
2
Since this is different in many other languages I was wondering why the Fortran standard committee chose this behavior?
Thanks a lot! Mike
This is mainly due to historical reasons. Old compilers (Fortran IV(66) and before) were implemented to produce programs using mainly static memory. Old machines even didn't have any stack. Therefore the programs behave, as the variables were defined as save
.
The predecessor of the variable initialization, the DATA
statement, is more like defining an initial content of static memory (similar to the directives for the data segment in assembly), than the on call variable initialization you may know from C. The syntax became similar to the C variant later.
这篇关于为什么Fortran中存在隐含的SAVE属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!