为什么 Fortran 中有隐含的 SAVE 属性? [英] Why is there an implied SAVE attribute in Fortran?

查看:37
本文介绍了为什么 Fortran 中有隐含的 SAVE 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Fortran 声明语句中初始化一个变量,该变量会隐式接收一个 SAVE 属性,并且初始化表达式只会执行一次.

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.

例如下面的程序

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

将打印

1
2

由于这在许多其他语言中都不同,我想知道为什么 Fortran 标准委员会选择了这种行为?

Since this is different in many other languages I was wondering why the Fortran standard committee chose this behavior?

非常感谢!迈克

推荐答案

这主要是历史原因.旧的编译器(Fortran IV(66) 和之前的编译器)主要使用静态内存来生成程序.旧机器甚至没有任何堆栈.因此程序的行为,因为变量被定义为 save.

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.

变量初始化的前身,DATA语句,更像是定义静态内存的初始内容(类似于汇编中数据段的指令),而不是调用变量初始化你可能从 C 中知道.后来语法变得类似于 C 变体.

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屋!

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